首页 > 代码库 > POJ 3723 Conscription

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

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 14634 1 24390 4 43733 4 88892 4 3133

Sample Output

7107154223

Source

POJ Monthly Contest – 2009.04.05
 
解题:最小生成树。
 
 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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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\n",10000*(n+m)+Kruskal());59     }60     return 0;61 }
View Code

 

POJ 3723 Conscription