Poj3723conscription Kruskal Template Water problem

Source: Internet
Author: User

//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, &AMP;EDGE[I].V, &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.