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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;
const int MAXN = 1010; const int INF = 0x3f3f3f3f;
int T, n, m, x, y, w; int father[MAXN], lowcost[MAXN], maxValue[MAXN][MAXN], mp[MAXN][MAXN], sta[MAXN]; bool vis[MAXN];
void init() { memset(vis, false, sizeof(vis)); memset(maxValue, 0, sizeof(maxValue)); memset(sta, 0, sizeof(sta)); for(int i = 1; i <= n; i++){ father[i] = 1; for(int j = 1; j <= n; j++) mp[i][j] = INF; } }
void solve() { int ans = 0, pos; for(int i = 1; i <= n; i++){ lowcost[i] = mp[1][i]; } vis[1] = true; int top = 0; sta[top++] = 1; for(int i = 1; i <= n; i++){ pos = -1; int mins = 0x3f3f3f3f; for(int j = 1; j <= n; j++){ if(!vis[j] && (pos == -1 || lowcost[j] < lowcost[pos])){ pos = j; mins = lowcost[pos]; } } if(pos == -1) break; ans += mins; vis[pos] = true;
for(int k = 0; k < top; k++){ maxValue[sta[k]][pos] = maxValue[pos][sta[k]] = max(mins, maxValue[sta[k]][father[pos]]); }
sta[top++] = pos
for(int j = 1; j <= n; j++){ if(!vis[j] && lowcost[j] > mp[pos][j]){ lowcost[j] = mp[pos][j]; father[j] = pos; } } }
int mins = INF; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++)if(i != j && mp[i][j] != INF && father[i] != j && father[j] != i){ if(mins > mp[i][j] - maxValue[i][j]) mins = mp[i][j] - maxValue[i][j]; } } if(mins == 0) printf("Not Unique!\n"); else printf("%d\n", ans); }
int main() { freopen("input.txt", "r", stdin); scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); init(); for(int i = 0; i < m; i++){ scanf("%d%d%d", &x, &y, &w); if(mp[x][y] > w) mp[x][y] = mp[y][x] = w; } solve(); }
return 0; }
|