POJ 3723 Conscription (maximum weight forest + Kruskal algorithm)

Source: Internet
Author: User

Conscription
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8317 Accepted: 2887

Description

Windy has a country, and he wants to build a army to protect his country. He has picked up N girls, and M Boys and wants to collect them to being his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There is some relationships between girls and boys and windy can use these relationships to reduce his cost. If girl x and boy y had a relationship D and one of them has been collected, windy can collect The other one with 10000-D RMB. Now given all the relationships between girls and boys, your assignment are to find the least amount of money windy have to Pay. Notice that is only one relationship can is used when collecting one soldier.

Input

The first line of input was the number of the test case.
The first line of all test case contains three integers, N, M and R.
Then R lines followed, each contains three integers xi, Yi and di.
There is a blank line before each test case.

1 ≤ N, M ≤10000
0≤ R ≤50,000
0≤ XI < N
0≤ Yi < M
0 < di < 10000

Output

For each test case output of the answer in a single line.

Sample Input

25 5 84 3 68311 3 45830 0 65920 1 30633 3 49751 3 20494 2 21042 2 7815 5 102 4 98203 2 62363 1 88642 4 83262 0 51562 0 146 34 1 24390 4 43733 4 88892 4 3133

Sample Output

7107154223

Source

POJ Monthly contest–2009.04.05, windy7926778



Test instructions: Now need to recruit female soldiers n people, male soldier M. It costs $10000 to recruit a person, but if there are some close-knit people who have been recruited, you can spend less money. Given the intimacy of a number of 1~9999 between men and women, the cost of recruiting someone is 10000-(the maximum number of people who have been recruited and their intimacy). The requirement that the appropriate recruitment order be made to minimize the cost of recruiting all persons.


Analysis: First imagine such a graph: when recruiting someone a, if you use the relationship between A and B, then even a side A to B edge. Assuming that there is a circle in this diagram, there is a contradiction in the order in which all the people in this circle are recruited. So you can tell that this is a forest. Conversely, if a forest is given, then the corresponding relationship group can be used to set the order of recruitment. Therefore, as a vertex, the relationship as a side, the problem can be translated into solving the problem of the maximum weight forest in the graph. The most powerful forest problem can be solved by using the least spanning tree algorithm after all the edge weights are reversed.




AC Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath > #include <cstdlib> #include <ctime> #include <stack>using namespace std; #define INF 0x7fffffff# Define LL long Long#define MID (A, B) A + (b-a)/2#define max_e 50000 + 10#define max_n 10000 + 10int Pa[2*max_n], rank[2*m       Ax_n];        And check the set void Init_union_find (int V) {for (int i=0; i<v; i++) {Pa[i] = i;    Rank[i] = 0; }}int find (int x) {return x = = Pa[x]? x:pa[x] = Find (Pa[x]);}    void unite (int x, int y) {x = find (x), y = find (y);        if (x! = y) {if (Rank[x] < rank[y]) pa[x] = y;            else{Pa[y] = x;        if (rank[x] = = Rank[y]) rank[x] + +; }}}bool Same (int x, int y) {return find (x) = = Find (y);} struct edge{int u, V, cost;}; BOOL CMP (Edge A, Edge b) {return A.cost <B.cost;}    Edge Es[max_e];int V, E;long long Kruskal () {//kruskal algorithm sort (es, es+e, CMP);    Init_union_find (V);    A long long ans = 0;        for (int i=0; i<e; i++) {Edge E = es[i];            if (!same (E.U, e.v)) {Unite (E.U, E.V);        Ans + = e.cost; }} return ans;    int N, M, R;int main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int T, x, y, D;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n, &m, &r);        V = N + M;        E = R;            for (int i=0; i<r; i++) {scanf ("%d%d%d", &x, &y, &d);                     ES[I].U = x;            These three sentences can also be converted to es[i] = (edge) {x, Y+n,-d}, but only in g++, C + + will be ce es[i].v = N + y;        Es[i].cost =-D;    } printf ("%lld\n", 10000 * (n + M) + Kruskal ()); } return 0;}




POJ 3723 Conscription (maximum weight forest + Kruskal algorithm)

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.