hdu1385Minimum Transport Cost-floyd+打印路径

hdu1385


肚子好饿!



#include<iostream>
#include<stdio.h>
#include<string.h>
#define M 500
#define INF 1000000000
using namespace std;

int mp[M][M],path[M][M],fee[M];

int main()
{
	int n,a,b,i,j,k,min;
	while(scanf("%d",&n),n)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				scanf("%d",&mp[i][j]);
				if(mp[i][j]==-1) mp[i][j]=INF;
				path[i][j]=j;
			}
		}
		for(i=1;i<=n;i++)	scanf("%d",&fee[i]);
	//	printf("\t");for(i=1;i<=n;i++)	printf("%d ",fee[i]);printf("\n");
		for(k=1;k<=n;k++)
		{
			for(i=1;i<=n;i++)
			{
				for(j=1;j<=n;j++)
				{
					if(mp[i][j]>mp[i][k]+mp[k][j]+fee[k])
					{
						mp[i][j]=mp[i][k]+mp[k][j]+fee[k];
						path[i][j]=path[i][k];
					}
					else if(mp[i][j]==mp[i][k]+mp[k][j]+fee[k])
					{
						if(path[i][j]>path[i][k])
							path[i][j]=path[i][k];
					}
				}
			}
		}
		while(scanf("%d%d",&a,&b),a+b+2)
		{
			printf("From %d to %d :\n",a,b);
			printf("Path: %d",a);
			for(i=a;i!=b;i=path[i][b])
				printf("-->%d",path[i][b]);
			printf("\nTotal cost : %d\n\n",mp[a][b]);
		}
	}
	return 0;
}


你可能感兴趣的:(BI,Path)