首页 > 其他 > 详细

Conscription(Kruskal)

时间:2018-08-31 12:57:25      阅读:178      评论:0      收藏:0      [点我收藏+]
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16957   Accepted: 5896

Description

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-dRMB. 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

大概题意就是征男女兵,然后一个兵就需要10000,而对应的男女兵有个亲密度的东西,当两者都在时候就可以减去这个值,减少花费,求最小花费,而且对应的关系只可以用一次,这就可以用来就最大生成树来求,只遍历一遍。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 50005
using namespace std;
struct mm{
int m;
int w;
int love;
}ma[MAX];
int n,m,r,f[MAX];
bool cmp(mm a,mm b){
return a.love>a.love;
}
int find(int a)
{
return f[a]==a?a:find(f[a]);
}
int kursh(){

int sum=0;
for(int i=0;i<m+n;i++)
f[i]=i;sort(ma,ma+r,cmp);
for(int i=0;i<r;i++){
int x1,x2;
x1=find(ma[i].w);
x2=find(ma[i].m);
if(x1!=x2){
f[x2]=x1;
sum+=ma[i].love;
}

}
return sum;
}
int main(){
int T;
while(~scanf("%d",&T)){
while(T--){
cin>>n>>m>>r;
for(int i=0;i<r;i++){
cin>>ma[i].w>>ma[i].m>>ma[i].love;
ma[i].m+=n;

}
cout<<(n+m)*10000-kursh()<<endl;
}
}
}

 

Conscription(Kruskal)

原文:https://www.cnblogs.com/Yum20/p/9565162.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!