LCA+最小生成树是错的
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #define maxn 310 using namespace std; const long long INF = 1e17; int pos[maxn][maxn]; long long map[maxn][maxn]; long long dis[maxn][maxn]; vector<int>ins; int get(int x, int y) {//神仙分治 if (pos[x][y] == 0) return 0; get(x, pos[x][y]); ins.push_back(pos[x][y]); get(pos[x][y], y); } int main() { int n, m; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) { map[i][j] = INF; } } scanf("%d %d", &n, &m); long long len; int be, en; for (int i = 1; i <= n; i++) map[i][i] = 0; for (int i = 0; i < m; i++){ scanf("%d %d %lld", &be, &en, &len); map[be][en] = map[en][be] = min(map[be][en], len); } memcpy(dis, map, sizeof(map)); long long ans = INF; for (int k = 1; k <= n; k++) { for (int i = 1; i < k; i++) { for (int j = i+1; j < k; j++) { if (ans > dis[i][j] + map[i][k] + map[k][j]) { ans = dis[i][j] + map[i][k] + map[k][j]; ins.clear(); ins.push_back(i); get(i, j); ins.push_back(j); ins.push_back(k); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (dis[i][j] > dis[i][k] + dis[k][j]) { pos[i][j] = k; dis[i][j] = dis[i][k] + dis[k][j]; } } } } if (ans == INF) { printf("No solution. "); } else { for (int i = 0; i < ins.size(); i++) { printf("%d ", ins[i]); } printf(" "); } return 0; }