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
2.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
2 years ago
#define INF 0x3f3f3f3f
2 years ago
/**
:floyd i , j kk
*/
int n, m;
int g[N][N];
2 years ago
int dis[N][N]; // dp结果数组
2 years ago
int path[N][N];
int ans[N];
int cnt;
2 years ago
int res = INF;
2 years ago
void floyd() {
for (int k = 1; k <= n; k++) {
2 years ago
// dp
2 years ago
for (int i = 1; i < k; i++) {
2 years ago
for (int j = i + 1; j < k; j++) { // i,j,k序号由小到大
if (res - dis[i][j] > g[i][k] + g[k][j]) { // 减法防溢出
2 years ago
res = dis[i][j] + g[i][k] + g[k][j];
2 years ago
2 years ago
int x = i, y = j;
cnt = 0; // 以前有过的路径也清空
2 years ago
while (x != y) {
2 years ago
ans[cnt++] = y;
y = path[i][y];
2 years ago
}
2 years ago
ans[cnt++] = x;
2 years ago
ans[cnt++] = k; // 序号最大的节点k
2 years ago
}
}
}
2 years ago
// floyd
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (dis[i][j] > dis[i][k] + dis[k][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
path[i][j] = path[k][j]; // 这咋还和我理解的不一样呢?
2 years ago
}
}
}
int main() {
while (cin >> n >> m) {
2 years ago
// 邻接矩阵初始化
for (int i = 1; i <= n; i++)
2 years ago
for (int j = 1; j <= n; j++) {
2 years ago
dis[i][j] = g[i][j] = INF;
2 years ago
path[i][j] = i; // 这里也是不一样,需要思考与整理
2 years ago
}
2 years ago
// 读入边
2 years ago
while (m--) {
int a, b, c;
cin >> a >> b >> c;
2 years ago
g[a][b] = g[b][a] = min(g[a][b], c);
dis[a][b] = dis[b][a] = g[a][b];
2 years ago
}
floyd();
2 years ago
if (res == INF) {
2 years ago
puts("No solution.");
continue;
}
2 years ago
for (int i = 0; i < cnt; i++) printf("%d%s", ans[i], (i == cnt - 1) ? "\n" : " ");
2 years ago
}
return 0;
}