1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> #include <algorithm> #include <cstring>
using namespace std;
typedef long long LL;
const int N = 110, INF = 0x3f3f3f3f;
int n, m; int d[N][N], g[N][N]; int path[N], pos[N][N]; int cnt;
void get_path(int l, int r) { int k = pos[l][r]; if (k == 0) return; get_path(l, k); path[cnt ++ ] = k; get_path(k, r); }
int main() { scanf("%d%d", &n, &m); memset(g, 0x3f, sizeof g); while (m -- ) { int a, b, c; scanf("%d%d%d", &a, &b, &c); g[a][b] = g[b][a] = min(g[a][b], c); } memcpy(d, g, sizeof g); int res = INF; for (int k = 1; k <= n; k ++ ) { for (int i = 1; i < k; i ++ ) for (int j = i + 1; j < k; j ++ ) if ((LL)d[i][j] + g[j][k] + g[k][i] < res) { res = d[i][j] + g[j][k] + g[k][i]; cnt = 0; path[cnt ++ ] = k; path[cnt ++ ] = i; get_path(i, j); path[cnt ++ ] = j; } for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) if (d[i][j] > d[i][k] + d[k][j]) { d[i][j] = d[i][k] + d[k][j]; pos[i][j] = k; } } if (res == INF) puts("No solution."); else { for (int i = 0; i < cnt; i ++ ) printf("%d ", path[i]); puts(""); } return 0; }
|