POJ 3723 Conscription

来源:互联网 发布:淘宝卖阿迪达斯的店铺 编辑:程序博客网 时间:2024/06/05 14:35

                                                                        Conscription
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14103 Accepted: 4898

Description

Windy has a country, and he wants to build an army to protect his country. He has picked upN 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, N, M andR.
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 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

题目大意:大佬要挑n个女生和m个男生到他的军队里面,每挑到一个人,就要给人家10000RMB,如果女孩x与男孩y有关系d且他们其中一个被招了,那么招另外一个就只用支付10000-d RMB了,算出最少的费用

最小生成树,用的是Kruskal算法

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int n,m,r;int tree[20010];struct Node{int u,v,w;}map[50010];bool cmp(Node a,Node b){return a.w>b.w;}int find(int x){return x==tree[x]?x:tree[x]=find(tree[x]);}int main(){int t;scanf("%d",&t);while(t--){cin>>n>>m>>r;for(int i=0;i<=n+m;i++) tree[i]=i;int kount=0;while(r--){int x,y,z;scanf("%d%d%d",&x,&y,&z);map[kount].u=x;map[kount].v=y+n;map[kount++].w=z;}sort(map,map+kount,cmp);int sum=(n+m)*10000;for(int i=0;i<kount;i++){int a=find(map[i].u);int b=find(map[i].v);if(a!=b){sum-=map[i].w;tree[a]=b;}}printf("%d\n",sum);}return 0;}


原创粉丝点击