标签:
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8085    Accepted Submission(s): 2105
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define INF 100000000
int n,dist[205][205],a[205],path[205][205];
void floyd()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            path[i][j]=j;
    }
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int temp=dist[i][k]+dist[k][j]+a[k];
                if(dist[i][j]>temp)
                {
                    dist[i][j]=temp;
                    path[i][j]=path[i][k];
                }
                else if(dist[i][j]==temp&&path[i][k]<path[i][j])
                {
                    path[i][j]=path[i][k];
                }
            }
        }
    }
}
int main()
{
    int u,v;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&dist[i][j]);
                if(dist[i][j]==-1)
                    dist[i][j]=INF;
            }
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        floyd();
        while(scanf("%d%d",&u,&v)!=EOF)
        {
            if(u==-1&&v==-1)
                break;
            printf("From %d to %d :\n",u,v);
            printf("Path: %d",u);
            int temp=u;
            while(u!=v)
            {
                printf("-->%d",path[u][v]);
                u=path[u][v];
            }
            printf("\n");
            printf("Total cost : %d\n\n",dist[temp][v]);
        }
    }
    return 0;
}
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4396634.html