首页 > 其他 > 详细

hdu 1395 Minimum Transport Cost

时间:2015-06-09 17:12:22      阅读:175      评论:0      收藏:0      [点我收藏+]
#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;
}

hdu 1395 Minimum Transport Cost

原文:http://blog.csdn.net/xinag578/article/details/46428381

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!