#include<iostream> #include<algorithm> #define N 26 usingnamespace std; int n, m, p[N]; char c; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { while (cin >> n, n) { m = 0; for (int i = 1, u, k, w; i < n; ++i) { cin >> c >> k; u = c - 'A'; while (k--) { cin >> c >> w; e[m++] = {w, u, c - 'A'}; } } cout << kruskal() << endl; } return0; }
#include<cstdio> #include<algorithm> usingnamespace std; constint N = 51, M = 1500; int n, m, p[N]; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[M]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 1; i <= n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { while (scanf("%d", &n), n) { scanf("%d", &m); for (int i = 0, a, b, c; i < m; ++i) { scanf("%d%d%d", &a, &b, &c); e[i] = {c, a, b}; } printf("%d\n", kruskal()); } return0; }
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> usingnamespace std; constint N = 100; int n, m, p[N]; double x[N], y[N], z[N], r[N]; doubleget_dist(int i, int j) { double d1 = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2)); double d2 = r[i] + r[j]; return d2 >= d1 ? 1e-9 : d1 - d2; } structedge { double w; int u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } doublekruskal() { double res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { while (scanf("%d", &n), n) { m = 0; for (int i = 0; i < n; ++i) scanf("%lf%lf%lf%lf", x + i, y + i, z + i, r + i); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) e[m++] = {get_dist(i, j), i, j}; printf("%.3lf\n", kruskal()); }
#include<cstdio> #include<algorithm> usingnamespace std; constint N = 100; int n, m, q, a, b, g[N][N], p[N]; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%d", &g[i][j]); scanf("%d", &q); while (q--) { scanf("%d%d", &a, &b); --a, --b; g[a][b] = g[b][a] = 0; } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) e[m++] = {g[i][j], i, j}; printf("%d\n", kruskal()); return0; }
#include<cstdio> #include<cstring> #include<algorithm> #define N 500 usingnamespace std; int t, n, m, price[N], g[N][N], p[N]; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", price + i); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%d", &g[i][j]); m = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) e[m++] = {g[i][j] + price[i] + price[j], i, j}; printf("%d\n", kruskal()); } return0; }
#include<cstdio> #include<algorithm> #define N 2000 usingnamespace std; int n, m, p[N]; char s[N][8]; intget_dist(int i, int j) { int cnt = 0; for (int k = 0; k < 7; ++k) if (s[i][k] != s[j][k]) ++cnt; return cnt; } structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { while (scanf("%d", &n), n) { m = 0; for (int i = 0; i < n; ++i) scanf("%s", s[i]); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) e[m++] = {get_dist(i, j), i, j}; printf("The highest possible quality is 1/%d.\n", kruskal()); } return0; }
#include<cstdio> #include<algorithm> #define N 2000 usingnamespace std; int n, g[N][N], d[N]; char s[N][8]; bool st[N]; intget_dist(int i, int j) { int cnt = 0; for (int k = 0; k < 7; ++k) if (s[i][k] != s[j][k]) ++cnt; return cnt; } intprim() { int res = 0; for (int i = 0; i < n; ++i) d[i] = 0x3f3f3f3f, st[i] = 0; d[0] = 0; for (int i = 0; i < n; ++i) { int t = -1; for (int j = 0; j < n; ++j) if (!st[j] && (t == -1 || d[j] < d[t])) t = j; st[t] = 1; if (i) res += d[t]; for (int j = 0; j < n; ++j) if (!st[j] && d[j] > g[t][j]) d[j] = g[t][j]; } return res; } intmain() { while (scanf("%d", &n), n) { for (int i = 0; i < n; ++i) scanf("%s", s[i]); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) g[i][j] = g[j][i] = get_dist(i, j); printf("The highest possible quality is 1/%d.\n", prim()); } return0; }
#include<cstdio> #include<cmath> #include<algorithm> #define N 500 usingnamespace std; int t, s, n, m, x[N], y[N], p[N]; doubleget_dist(int i, int j) { returnsqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)); } structedge { double w; int u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } doublekruskal() { double res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0, cnt = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) { p[a] = b, res = e[i].w, ++cnt; if (cnt + s >= n) // (cnt + 1) + (s - 1) >= n break; } } return res; } intmain() { scanf("%d", &t); while (t--) { scanf("%d%d", &s, &n); for (int i = 0; i < n; ++i) scanf("%d%d", x + i, y + i); if (n <= s) printf("%.2lf", 0); else { m = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) e[m++] = {get_dist(i, j), i, j}; printf("%.2lf\n", kruskal()); } } return0; }
#include<cstdio> #include<cmath> #include<algorithm> #define N 751 usingnamespace std; int n, m, a, b, x[N], y[N], p[N]; doubleget_dist(int i, int j) { returnsqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)); } structedge { double w; int u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } voidkruskal() { sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) { p[a] = b; printf("%d %d\n", e[i].u, e[i].v); } } } intmain() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i); for (int i = 1; i <= n; ++i) p[i] = i; scanf("%d", &m); while (m--) { scanf("%d%d", &a, &b); p[find(a)] = find(b); } m = 0; for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) e[m++] = {get_dist(i, j), i, j}; kruskal(); return0; }
#include<cstdio> #include<cmath> #include<algorithm> #define N 100 usingnamespace std; int n, m, g[N][N], p[N]; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { while (~scanf("%d", &n)) { m = 0; for (int i = 0, w; i < n; ++i) for (int j = 0; j < n; ++j) { scanf("%d", &w); if (i > j) e[m++] = {w, i, j}; } printf("%d\n", kruskal()); } return0; }
#include<iostream> #include<algorithm> usingnamespace std; constint N = 50, M = 10000; int t, r, c, n, m, p[N * N], ds[] = {1, 0, -1, 0, 1}; string maze[N]; bool st[N][N]; int q[N * N][2], hh, tt, w; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[M]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 0; i < r * c; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } voidbfs(int x, int y) { int src = x * c + y; hh = tt = 0; q[tt][0] = x, q[tt][1] = y, tt++; for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) st[i][j] = 0; st[x][y] = 1; maze[x][y] = ' '; w = 0; while (tt - hh) { ++w; int sz = tt - hh; while (sz--) { int a = q[hh][0], b = q[hh][1]; ++hh; for (int i = 0; i < 4; ++i) { int nx = a + ds[i], ny = b + ds[i + 1]; if (nx < 0 || nx >= r || ny < 0 || ny >= c || // maze[nx][ny] == '#' || st[nx][ny]) continue; if (maze[nx][ny] != ' ') e[m++] = {w, src, nx * c + ny}; q[tt][0] = nx, q[tt][1] = ny, tt++; st[nx][ny] = 1; } } } } intmain() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> c >> r; getline(cin, maze[0]); // consume '\n'. for (int i = 0; i < r; ++i) getline(cin, maze[i]); n = m = 0; for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) if (maze[i][j] == 'S' || maze[i][j] == 'A') ++n, bfs(i, j); cout << kruskal() << endl; } return0; }
#include<cstdio> #include<algorithm> #define N 101 usingnamespace std; int t, n, m, p[N]; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } voidkruskal() { int sum = 0, res = 0; for (int i = 1; i <= n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int r = i; while (r < m && e[i].w == e[r].w) ++r; --r; for (int j = i; j <= r; ++j) { int a = find(e[j].u), b = find(e[j].v);
if (a != b) sum += e[j].w; } for (int j = i; j <= r; ++j) { int a = find(e[j].u), b = find(e[j].v); if (a != b) p[a] = b, res += e[j].w; } i = r; } if (sum != res) printf("Not Unique!\n"); else printf("%d\n", res); } intmain() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); kruskal(); } return0; }
#include<cstdio> #include<cmath> #include<algorithm> #define N 101 usingnamespace std; int n, m, p[N]; structedge { int w, u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } intkruskal() { int res = 0; for (int i = 1; i <= n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); if (a != b) p[a] = b, res += e[i].w; } return res; } intmain() { while (~scanf("%d", &n) && n) { m = n * (n - 1) / 2; for (int i = 0, w; i < m; ++i) scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w); printf("%d\n", kruskal()); } return0; }
#include<cstdio> #include<cmath> #include<algorithm> #define N 100 usingnamespace std; int t, n, m, x[N], y[N], p[N]; doubleget_dist(int i, int j) { returnsqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)); } structedge { double w; int u, v; booloperator<(const edge &a) const { return w < a.w; } } e[N * N]; intfind(int x) { return p[x] == x ? x : p[x] = find(p[x]); } voidkruskal() { double res = 0; int cnt = 0; for (int i = 0; i < n; ++i) p[i] = i; sort(e, e + m); for (int i = 0; i < m; ++i) { int a = find(e[i].u), b = find(e[i].v); double w = e[i].w; if (a != b && 10 <= w && w <= 1000) p[a] = b, res += e[i].w, ++cnt; if (cnt + (m - i - 1) < n - 1) break; } if (cnt == n - 1) printf("%.1lf\n", res * 100); else printf("oh!\n"); } intmain() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d%d", x + i, y + i); m = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) e[m++] = {get_dist(i, j), i, j}; kruskal(); } return0; }