标签:
1 #include<stdio.h> 2 #include<string.h> 3 const int INF=0x3f3f3f3f; 4 const int MAXN=1010; 5 #define MIN(x,y) (x<y?x:y) 6 int map[MAXN][MAXN]; 7 int vis[MAXN],d[MAXN]; 8 int N; 9 void initial(){ 10 memset(d,INF,sizeof(d)); 11 memset(vis,0,sizeof(vis)); 12 memset(map,INF,sizeof(map)); 13 } 14 void dijkscra(int s){ 15 int k; 16 d[s]=0; 17 while(true){ 18 k=-1; 19 for(int i=1;i<=N;i++) 20 if(!vis[i]&&(k==-1||d[i]<d[k]))k=i; 21 if(k==-1)break; 22 vis[k]=1; 23 for(int i=1;i<=N;i++){ 24 d[i]=MIN(d[i],d[k]+map[k][i]); 25 } 26 } 27 } 28 int main(){ 29 int M; 30 int a,b,c; 31 while(~scanf("%d%d",&N,&M),N||M){ 32 initial(); 33 while(M--){ 34 scanf("%d%d%d",&a,&b,&c); 35 if(c<map[a][b]){ 36 map[a][b]=map[b][a]=c; 37 } 38 } 39 dijkscra(1); 40 printf("%d\n",d[N]); 41 } 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4737543.html