hdu1385

来源:互联网 发布:免费视频字幕制作软件 编辑:程序博客网 时间:2024/05/22 13:43

重点是打印路径的方法

#include<iostream>#include<algorithm>#include<queue>#include<vector>#define MAX  10000#define INF  0X3f3f3f3fusing namespace std;int G[MAX][MAX];int dp[MAX][MAX];int path[MAX][MAX];int tax[MAX];int n;void Floyd(){for (int i = 0; i < n; i++)for (int j = 0; j < n; j++){dp[i][j] = G[i][j];path[i][j]=j;}for (int k = 0; k < n; k++)for (int i = 0; i < n; i++)for (int j = 0; j < n; j++){        if (dp[i][j] > dp[i][k] + dp[k][j] + tax[k]){dp[i][j] = dp[i][k] + dp[k][j] + tax[k];path[i][j] = path[i][k]; }else if (dp[i][j] == (dp[i][k] + dp[k][j] + tax[k]) && path[i][j]>path[i][k]){path[i][j] = path[i][k];}//字典序 坑的一批这里}}int main(){while (cin >> n,n){for (int i = 0; i < n; i++)for (int j = 0; j < n; j++){   int tmp;cin >>tmp;if (tmp==-1)tmp = INF;G[i][j] = tmp;}for (int i = 0; i < n; i++)cin >> tax[i];int b, e;Floyd();while (cin >> b >> e){if (b == -1 || e == -1)break;int tmp=b-1;cout << "From" << " " << b << " " << "to" << " " << e << " " << ":" << endl;cout << "Path: "<<b;while (tmp!= (e-1)){cout << "-->" << path[tmp][e-1]+1;tmp = path[tmp][e-1];}cout << endl;cout <<"Total cost : "<< dp[b-1][e-1] << endl<<endl;}}}


0 0
原创粉丝点击