标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42849 Accepted Submission(s): 18784
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MAX 0x3f3f3f3f 5 using namespace std; 6 int map[110][110]; 7 int d[110],m,n; 8 void dijkstra() 9 { 10 int i,j,used[110]; 11 for(i=1;i<=m;i++) 12 { 13 d[i]=map[1][i]; 14 used[i]=0; 15 } 16 used[1]=1; 17 for(i=2;i<=m;i++) 18 { 19 int min=MAX,mark; 20 for(j=1;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=1;j<=m;j++) 32 { 33 if(d[j]>d[mark]+map[mark][j]&&!used[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),m||n) 42 { 43 memset(map,MAX,sizeof(map)); 44 for(i=1;i<=n;i++) 45 { 46 scanf("%d%d%d",&a,&b,&c); 47 map[a][b]=map[b][a]=c; 48 } 49 dijkstra(); 50 printf("%d\n",d[m]); 51 } 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4739661.html