标签:span cst bsp 最短路 dijkstra printf memset ios can
/*
矩阵太神奇了Orz,网上的题解大多是spfa,不过我发想dijkstra也能做
把n*n的矩阵看成是单向边距离矩阵就行
*/
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #define MAXN 500 #define INF 1<<30 using namespace std; int a[MAXN][MAXN]; int n; int dist[MAXN],vis[MAXN]; int dijkstra(int s,int t){ memset(dist,0,sizeof dist); memset(vis,0,sizeof vis); for(int i=0;i<=n;i++) if(i==s) dist[i]=INF; else dist[i]=a[s][i]; while(1){ int k=-1; int Min=INF; for(int i=1;i<=n;i++) if(!vis[i]&&dist[i]<Min){ Min=dist[i]; k=i; } if(k==-1) break; vis[k]=true; for(int i=1;i<=n;i++) if(!vis[i]&&dist[i]>dist[k]+a[k][i]) dist[i]=dist[k]+a[k][i]; } return dist[t]; } int main(){ while(scanf("%d",&n)==1){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); printf("%d\n",min(dijkstra(1,n),dijkstra(1,1)+dijkstra(n,n))); } return 0; }
hdu4370 dijkstra矩阵转单向边最短路矩阵+自环闭环
标签:span cst bsp 最短路 dijkstra printf memset ios can
原文地址:https://www.cnblogs.com/zsben991126/p/9797207.html