标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 36946 Accepted Submission(s): 13591
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MAX 0x3f3f3f3f 5 using namespace std; 6 int map[300][300],m,n,flag; 7 int d[300]; 8 void dijkstra(int x) 9 { 10 int i,j,used[300]; 11 for(i=0;i<m;i++) 12 { 13 used[i]=0; 14 d[i]=map[x][i]; 15 } 16 used[x]=1; 17 for(i=0;i<m;i++) 18 { 19 int min=MAX,mark; 20 for(j=0;j<m;j++) 21 { 22 if(!used[j]&&d[j]<min) 23 { 24 min=d[j]; 25 mark=j; 26 } 27 } 28 if(min==MAX) 29 break; 30 used[mark]=1; 31 for(j=0;j<m;j++) 32 { 33 if(d[j]>d[mark]+map[mark][j]) 34 d[j]=d[mark]+map[mark][j]; 35 } 36 } 37 } 38 int main() 39 { 40 int i,j,a,b,c; 41 while(scanf("%d%d",&m,&n)!=EOF) 42 { 43 44 memset(map,MAX,sizeof(map)); 45 for(i=0;i<n;i++) 46 { 47 scanf("%d%d%d",&a,&b,&c); 48 if(map[a][b]>c) 49 map[a][b]=map[b][a]=c; 50 map[i][i]=0; 51 } 52 int x,y; 53 flag=0; 54 scanf("%d%d",&x,&y); 55 dijkstra(x); 56 if(d[y]==MAX) 57 printf("-1\n"); 58 else 59 printf("%d\n",d[y]); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4739640.html