//n girl, M-Boy, the cost of hiring a man is 10000//The relationship between boys and girls is D, so if one is hired, the cost of hiring another is 10000-d//Everyone can only pass a relationship offer//Ask at least how much it takes to hire these people//The most obvious spanning tree#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const intMAXN =50010;structnode{intU, V, W;} EDGE[MAXN];BOOLCMP (Node A, Node B) {returnA.W > B.W;}intf[maxn*2] ;intFindintx) {if(F[x] = =-1)returnx;returnF[X] = find (F[x]);}intR;intN, M;intKruskal () {intAns =0; for(inti =1; I <= r;i++) {intFu = find (edge[i].u);intFV = Find (EDGE[I].V);if(FU = FV)Continue; Ans + = EDGE[I].W; F[FU] = FV; }returnAns;}intMain () {//freopen ("In.txt", "R", stdin); intt;scanf("%d", &t); while(t--) {scanf("%d%d%d", &n, &m, &r);memset(F,-1,sizeof(F)); for(inti =1; I <= r;i++) {scanf("%d%d%d", &edge[i].u, &EDGE[I].V, &EDGE[I].W); EDGE[I].V + = MAXN; } Sort (Edge +1, Edge +1+ R, CMP);intans = Kruskal ();cout<< (n+m) *10000-ans<<endl; }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj3723conscription Kruskal Template Water problem