Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3425 Accepted Submission(s): 1397
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
1 #include<stdio.h> 2 #include<string.h> 3 #define MIN(x,y)(x<y?x:y) 4 const int INF=0x3f3f3f; 5 const int MAXN=110; 6 int map[MAXN][MAXN],dis[MAXN][MAXN],anser; 7 int N,M; 8 void initial(){ 9 for(int i=1;i<=100;i++) 10 for(int j=1;j<=100;j++) 11 if(i-j)map[i][j]=INF; 12 else map[i][j]=0; 13 } 14 void floyd(){ 15 for(int i=1;i<=N;i++) 16 for(int j=1;j<=N;j++) 17 dis[i][j]=map[i][j]; 18 anser=INF; 19 for(int k=1;k<=N;k++){ 20 for(int i=1;i<=N;i++) 21 for(int j=1;j<=N;j++) 22 if(i!=j&&i!=k&&j!=k) 23 anser=MIN(anser,dis[i][j]+map[j][k]+map[k][i]); 24 /*if(anser>dis[i][j]+map[j][k]+map[k][i]){ 25 printf("dis[%d][%d]=%d\n",i,j,dis[i][j]); 26 printf("map[%d][%d]=%d\n",j,k,map[j][k]); 27 printf("map[%d][%d]=%d\n",k,i,map[k][i]); 28 }*/ 29 for(int i=1;i<=N;i++) 30 for(int j=1;j<=N;j++) 31 dis[i][j]=MIN(dis[i][j],dis[i][k]+dis[k][j]);//弄成maple错了半天。。。。 32 } 33 if(anser==INF)puts("It's impossible."); 34 else printf("%d\n",anser); 35 } 36 int main(){ 37 int a,b,c; 38 while(~scanf("%d%d",&N,&M)){ 39 initial(); 40 while(M--){ 41 scanf("%d%d%d",&a,&b,&c); 42 if(c<map[a][b])map[a][b]=map[b][a]=c; 43 } 44 floyd(); 45 } 46 return 0; 47 }