POJ 3723 Conscription

Conscription

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3723
64-bit integer IO format: %lld      Java class name: Main
 

Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have 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 is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

 

Input

The first line of input is the number of test case.The first line of each test case contains three integers, NM and R.Then R lines followed, each contains three integers xiyi and di.There is a blank line before each test case.

1 ≤ NM ≤ 10000

0 ≤ R ≤ 50,000

0 ≤ xi < N

0 ≤ yi < M

0 < di < 10000

 

Output

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

Sample Input

2

5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781

5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133

Sample Output

71071
54223

Source

 
解题:最小生成树。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 50010;
18 struct arc{
19     int x,y,w;
20     arc(int a = 0,int b = 0,int c = 0){
21         x = a;
22         y = b;
23         w = c;
24     }
25 };
26 arc e[maxn];
27 int uf[maxn],n,m,r,tot;
28 bool cmp(const arc &a,const arc &b){
29     return a.w < b.w;
30 }
31 int Find(int x){
32     if(x != uf[x]) uf[x] = Find(uf[x]);
33     return uf[x];
34 }
35 int Kruskal(){
36     for(int i = 0; i <= n+m; i++) uf[i] = i;
37     int ans = 0;
38     sort(e,e+r,cmp);
39     for(int i = 0; i < r; i++){
40         int tx = Find(e[i].x);
41         int ty = Find(e[i].y);
42         if(tx != ty){
43             ans += e[i].w;
44             uf[tx] = ty;
45         }
46     }
47     return ans;
48 }
49 int main() {
50     int t,u,v,w;
51     scanf("%d",&t);
52     while(t--){
53         scanf("%d %d %d",&n,&m,&r);
54         for(int i = 0; i < r; i++){
55             scanf("%d %d %d",&u,&v,&w);
56             e[i] = arc(u,n+v,-w);
57         }
58         printf("%d
",10000*(n+m)+Kruskal());
59     }
60     return 0;
61 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3958725.html