Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20436 Accepted Submission(s): 7092
|
flody注意下重边以及双向边
#include<stdio.h> #include<string.h> #define INF 0x3f3f3f3f int dis[105][105]; int Max(int a,int b) { return a<b?a:b; } int main() { int n,m,i,j,k,x,y,w,s,t; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(i=0;i<n;i++) for(j=0;j<n;j++) dis[i][j]=(i==j)?0:INF; for(i=0;i<m;i++) { scanf("%d %d %d",&x,&y,&w); if(dis[x][y]>w) dis[x][y]=dis[y][x]=w; } for(k=0;k<n;k++) for(i=0;i<n;i++) if(dis[i][k]<INF) { for(j=0;j<n;j++) dis[i][j]=Max(dis[i][j],dis[i][k]+dis[k][j]); } scanf("%d %d",&s,&t); if(dis[s][t]<INF) printf("%d ",dis[s][t]); else printf("-1 "); } return 0; }