标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 44889 Accepted Submission(s): 16651
#include<stdio.h> #include<string.h> #define max 999999 int map[201][201]; int n,m,st,en,f[201],mark[201]; void Dijkstra() { int i,j,k,min; memset(mark,0,sizeof(mark)); for(i=0;i<n;i++) f[i]=map[st][i]; f[st]=0; for(i=0;i<n;i++) { min=max; for(j=0;j<n;j++) { if(!mark[j]&&f[j]<min) { min=f[j]; k=j; } } if(min==max)break; mark[k]=1; for(j=0;j<n;j++) if(!mark[j]&&f[j]>f[k]+map[k][j]) f[j]=f[k]+map[k][j]; } if(f[en]!=max)printf("%d\n",f[en]); else printf("-1\n"); } int main() { int x,y,z,i,j; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<=n-1;i++) for(j=0;j<=n-1;j++) map[i][j]=max; for(i=1;i<=m;i++) { scanf("%d %d %d",&x,&y,&z); if(map[x][y]>z) {map[x][y]=map[y][x]=z;} } scanf("%d %d",&st,&en); Dijkstra(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/wangmenghan/p/5690248.html