You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
2 years ago
const int N = 110;
2 years ago
const int INF = 0x3f3f3f3f;
2 years ago
// Floyd+记录起点后继
int n;
int g[N][N], w[N];
int path[N][N]; // 记录i到j最短路径中i的后继
2 years ago
void floyd() {
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
2 years ago
for (int j = 1; j <= n; j++) {
if (g[i][j] > g[i][k] + g[k][j] + w[k]) {
g[i][j] = g[i][k] + g[k][j] + w[k];
path[i][j] = path[i][k]; // i->j这条最短路径上i后面第一个节点是i->k路径上第一个节点
2 years ago
}
2 years ago
// 相同路径下选择后继更小的(为了字典序)
if (g[i][j] == g[i][k] + g[k][j] + w[k])
if (path[i][j] > path[i][k])
path[i][j] = path[i][k];
}
}
// 递归输出路径
void print(int s, int e) {
printf("-->%d", path[s][e]); // 输出s的后继
if (path[s][e] != e) // 如果不是直连
print(path[s][e], e); // 递归输出后继
2 years ago
}
2 years ago
/*
From 1 to 3 :
Path: 1-->5-->4-->3
Total cost : 21
2 years ago
2 years ago
From 3 to 5 :
Path: 3-->4-->5
Total cost : 16
From 2 to 4 :
Path: 2-->1-->5-->4
Total cost : 17
*/
2 years ago
int main() {
2 years ago
#ifndef ONLINE_JUDGE
freopen("HDU1385.in", "r", stdin);
#endif
2 years ago
while (cin >> n, n) {
for (int i = 1; i <= n; i++)
2 years ago
for (int j = 1; j <= n; j++) {
2 years ago
cin >> g[i][j];
if (g[i][j] == -1) g[i][j] = INF;
path[i][j] = j;
2 years ago
}
2 years ago
2 years ago
for (int i = 1; i <= n; i++) cin >> w[i];
2 years ago
floyd();
2 years ago
2 years ago
int s, e;
2 years ago
2 years ago
while (cin >> s >> e, ~s && ~e) {
printf("From %d to %d :\n", s, e);
printf("Path: %d", s);
if (s != e) print(s, e); // 起点与终点不同开始递归
printf("\nTotal cost : %d\n\n", g[s][e]);
2 years ago
}
}
return 0;
}