hdu 1395 Minimum Transport Cost

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=100;
const int inf=1<<29;
int edge[N][N],path[N][N],b[N],p[N];

int main()
{
    int n,i,j,k,s,t,tmp;
    while(~scanf("%d",&n)&&n)
    {
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                scanf("%d",&edge[i][j]);
                if(edge[i][j]==-1) edge[i][j]=inf;
                path[i][j]=j;
            }

        for(i=1; i<=n; i++)
        {
            scanf("%d",&b[i]);
        }

        for(k=1; k<=n; k++)
            for(i=1; i<=n; i++)
            {
                for(j=1; j<=n; j++)
                {
                    if(edge[i][k]==inf||edge[k][j]==inf) continue;
                    tmp=b[k]+edge[i][k]+edge[k][j];
                    if(edge[i][j]>tmp)
                    {
                        edge[i][j]=tmp;
                        path[i][j]=path[i][k];
                    }
                    else if(edge[i][j]==tmp&&path[i][k]<path[i][j])
                    {
                        path[i][j]=path[i][k];
                    }
                }
            }
        while(~scanf("%d%d",&s,&t))
        {
            if(s==-1&&t==-1) break;
            printf("From %d to %d :\n",s,t);
            k=s;
            printf("Path: %d",k);
            while(k!=t)
            {
                printf("-->%d",path[k][t]);
                k=path[k][t];
            }
            printf("\nTotal cost : %d\n\n",edge[s][t]);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-06-09 16:28  xryz  阅读(97)  评论(0编辑  收藏  举报