您的位置:首页 > 其它

hdu 1385 Minimum Transport Cost

2014-04-18 21:37 316 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1385

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=9999999;

int g[maxn][maxn],tax[maxn],pre[maxn][maxn];
int m,n,s,e;

void inti()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i==j) g[i][j]=0;
else g[i][j]=inf;
}
}
}

void floyd()
{
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(g[i][j]>(g[i][k]+g[k][j]+tax[k]))
{
g[i][j]=g[i][k]+g[k][j]+tax[k];
pre[i][j]=pre[i][k];
}
else if(g[i][j]==g[i][k]+g[k][j]+tax[k])
{
if(pre[i][j]>pre[i][k])
pre[i][j]=pre[i][k];
}
}
}
}
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
inti();
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%d",&m);
if(m!=-1)
g[i][j]=m;
pre[i][j]=j;
}
}
for(int i=1; i<=n; i++)
{
scanf("%d",&tax[i]);
}
floyd();
scanf("%d%d",&s,&e);
while(1)
{
if(s==-1&&e==-1) break;
printf("From %d to %d :\n",s,e);
printf("Path: %d",s);
int s1=s;
while(s!=e)
{
printf("-->%d",pre[s][e]);
s=pre[s][e];
}
printf("\n");
printf("Total cost : %d\n",g[s1][e]);
scanf("%d %d",&s,&e);
printf("\n");
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: