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.

73 lines
2.0 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
#define INF 0x7ffffff
/**
:floyd i , j kk
INFwa 1e9
*/
int n, m;
int g[N][N];
2 years ago
int dis[N][N];
2 years ago
int path[N][N];
int ans[N];
int cnt;
int mm;
void floyd() {
mm = INF;
for (int k = 1; k <= n; k++) {
2 years ago
// dp
2 years ago
for (int i = 1; i < k; i++) {
for (int j = i + 1; j < k; j++) {
2 years ago
int x = dis[i][j] + g[k][i] + g[k][j];
2 years ago
if (x < mm) {
mm = x;
2 years ago
int tg = j;
2 years ago
cnt = 0;
2 years ago
while (tg != i) {
ans[cnt++] = tg;
tg = path[i][tg];
2 years ago
}
ans[cnt++] = i;
ans[cnt++] = k;
}
}
}
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
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();
if (mm == INF) {
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;
}