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
| #include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5; int n, m, tot, cnt, hi[N], h[N], pre[N], vis[N];
struct Node { int from, to, v, next; } g[N], E[N];
void add1(int x, int y, int z) { g[tot].from = x, g[tot].to = y, g[tot].v = z, g[tot].next = h[x], h[x] = tot++; }
void add2(int x, int y, int z) { cnt++, E[cnt].from = x, E[cnt].to = y, E[cnt].v = z; }
bool cmp(Node x, Node y) { if (hi[x.to] == hi[y.to]) return x.v < y.v; return hi[x.to] > hi[y.to]; }
int xfind(int x) { int i = x, j = x, temp; while (i != pre[i]) i = pre[i]; while (i != j) { temp = pre[j]; pre[j] = i; j = temp; } return i; }
void bfs() { int res1 = 1; queue<int> q; vis[1] = true; q.push(1); while (q.size()) { int u = q.front(); q.pop(); for (int i = h[u]; ~i; i = g[i].next) { int v = g[i].to; add2(u, v, g[i].v); if (!vis[v]) { vis[v] = true; res1++; q.push(v); } } } printf("%d ", res1); }
void Kruscal() { long long res2 = 0; int num = 0; for (int i = 1; i <= cnt; i++) { int x = E[i].from, y = E[i].to; int fx = xfind(x), fy = xfind(y); if (fx != fy) { pre[fx] = fy; res2 += E[i].v; num++; } if (num == n - 1) break; } printf("%lld\n", res2); }
int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &hi[i]); pre[i] = i; } memset(h, -1, sizeof(h)); for (int i = 1; i <= m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); if (hi[x] >= hi[y]) add1(x, y, z); if (hi[x] <= hi[y]) add1(y, x, z); } bfs(); sort(E + 1, E + cnt + 1, cmp); Kruscal(); return 0; }
|