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
|
#include<cstdio> #include<algorithm> #define MAX 1000001
struct Edge { int v1, v2; int weight; }edge[MAX];
int p[MAX]; inline int find(int a);
int kruskal(int N, int E); int main() { int N; bool first = true; while (scanf("%d", &N) != EOF) { int i, a, b, w; int old_cost = 0, new_cost; for (i = 1; i < N; i++) { scanf("%d%d%d", &a, &b, &w); old_cost += w; }
int K, M; scanf("%d", &K); for (i = 0; i < K; i++) scanf("%d%d%d", &edge[i].v1, &edge[i].v2, &edge[i].weight);
scanf("%d", &M); for (i = 0; i < M; i++) scanf("%d%d%d", &edge[i + K].v1, &edge[i + K].v2, &edge[i + K].weight);
new_cost = kruskal(N, K + M);
if (first) first = false; else putchar('\n'); printf("%d\n%d\n", old_cost, new_cost); }
return 0; } int kruskal(int N, int E) { int i; for (i = 0; i <= N; i++) p[i] = i;
std::sort(edge, edge + E, [](const Edge& a, const Edge& b)->bool{return a.weight < b.weight; } );
int e, cost = 0; for (i = 0, e = 0; i < E&&e < N - 1; e++, i++) { while (find(edge[i].v1) == find(edge[i].v2))i++;
p[find(edge[i].v1)] = find(edge[i].v2);
cost += edge[i].weight; }
return cost; } int find(int a) { return a == p[a] ? a : (p[a] = find(p[a])); }
|