hdu 1385 Minimum Transport Cost Floyd

来源:互联网 发布:ambari源码 编辑:程序博客网 时间:2024/06/16 03:38

In this problem,First I use dijkstra to solve it.But WA defeated me.I finally use Flyod.

The portal:http://acm.hdu.edu.cn/showproblem.php?pid=1385

#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXN 1005const int INF = 0x3f3f3f3f;int cost[MAXN][MAXN];int route[MAXN][MAXN];int power[MAXN];void Input(){int n;while(scanf("%d",&n),n){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){scanf("%d",cost[i]+j);if(cost[i][j]==-1){cost[i][j] = INF;}route[i][j] = j;}}for(int i=1;i<=n;i++){scanf("%d",power+i);}for(int k=1;k<=n;k++){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int temp = cost[i][k] + cost[k][j] + power[k] ;if(temp < cost[i][j]){cost[i][j] = temp;route[i][j] = route[i][k];}else if(temp == cost[i][j]){if(route[i][j] > route[i][k]){route[i][j] = route[i][k];}}}}}int g,h;while(scanf("%d %d",&g,&h),g+h+2){        printf("From %d to %d :\n",g,h);              printf("Path: %d",g);              int temp=g;              while(temp!=h)              {                  printf("-->%d",route[temp][h]);                  temp=route[temp][h];              }              printf("\n");              printf("Total cost : %d\n\n",cost[g][h]);   }}}int main(){//freopen("a.in","r",stdin);Input();return 0;}



0 0