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