hdu1385 flody+记录路径

来源:互联网 发布:黑暗启示录2bt版java 编辑:程序博客网 时间:2024/05/17 01:10
#include <iostream>#include <cstdio>using namespace std;const int maxn = 1234;const int _max = 0x3f3f3f3f;int dist[maxn][maxn];int dir[maxn][maxn];int a[maxn];int n, k;void flody(){    for(int k = 1; k <= n; k++)        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++)                if(dist[i][j] > dist[i][k] + dist[k][j] + a[k])                {                    dist[i][j] = dist[i][k] + dist[k][j] + a[k];                    dir[i][j] = dir[i][k];                }                else if(dist[i][j] == dist[i][k]+dist[k][j]+a[k] && dir[i][j]>dir[i][k])                    dir[i][j] = dir[i][k];}void output(){    int u, v;    while(scanf("%d %d", &u, &v))    {        if(u==-1&&v==-1) break;        int m = u;        printf("From %d to %d :\n", u, v);        printf("Path: %d", u);        while(u!=v)        {            printf("-->%d", dir[u][v]);            u = dir[u][v];        }        printf("\n");        printf("Total cost : %d\n\n", dist[m][v]);    }}int main(){    while(scanf("%d", &n) != EOF && n)    {        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] = _max;                dir[i][j] = j;            }        for(int i = 1; i <= n; i++)            scanf("%d", &a[i]);        flody();        output();    }}