Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31423 Accepted Submission(s): 11469
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
#include<stdio.h> #include<string.h> #define INF 0x3f3f3f #define max 200+10 int dist[max],visit[max],map[max][max]; int city,road; int start,end; void dijkstra() { int i,j,next,mindist; memset(visit,0,sizeof(visit)); for(i=0;i<city;i++) { dist[i]=map[start][i]; } visit[start]=1; for(i=1;i<city;i++) { mindist=INF; for(j=0;j<city;j++) { if(visit[j]==0&&mindist>dist[j]) { mindist=dist[j]; next=j; } } visit[next]=1; for(j=0;j<city;j++) { if(visit[j]==0&&dist[next]+map[next][j]<dist[j]) { dist[j]=dist[next]+map[next][j]; } } } if(dist[end]==INF) printf("-1\n"); else printf("%d\n",dist[end]); } int main() { int i,j,x,y,c; while(scanf("%d%d",&city,&road)!=EOF) { for(i=0;i<city;i++) { for(j=0;j<city;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } while(road--) { scanf("%d%d%d",&x,&y,&c); if(map[x][y]>c)//有重边 { map[x][y]=map[y][x]=c; } } scanf("%d%d",&start,&end); dijkstra(); } return 0; }
#include<stdio.h> #define INF 0x3f3f3f #define max 200+10 int map[max][max]; int city,road; int start,end; void floyd() { int k,i,j; for(k=0;k<city;k++) { for(i=0;i<city;i++) { for(j=0;j<city;j++) { if(map[i][j]>map[i][k]+map[k][j]) { map[i][j]=map[i][k]+map[k][j]; } } } } if(map[start][end]==INF) printf("-1\n"); else printf("%d\n",map[start][end]); } int main() { int i,j,x,y,c; while(scanf("%d%d",&city,&road)!=EOF) { for(i=0;i<city;i++) { for(j=0;j<city;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } while(road--) { scanf("%d%d%d",&x,&y,&c); if(map[x][y]>c) { map[x][y]=map[y][x]=c; } } scanf("%d%d",&start,&end); floyd(); } return 0; }