poj 3723 Conscription //最小生成森林
Conscription
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18414 Accepted: 6374 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-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 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 < 10000Output
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 3133Sample Output
71071 54223Source
POJ Monthly Contest – 2009.04.05, windy7926778
题意:求招募n个男兵,m个女兵的最小花费,其中有r个关系,男xi如果已经被招募,那么女yi招募的时候少花di钱。
如果不靠关系,那么答案显然,
再减去可以靠关系得到的最大缩减就行了。
最大缩减,可以将少花点钱变为 -di ,用克鲁斯卡尔的思想,求出最小森林即可。
输入巨大,cin+关闭同步是要被被无情T的,浪费时间搞这一点。。。。。 换成scanf即可。。。。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<climits>
#include<cmath>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define mod 1000000007
const int max_n=100005;
const int N=1e4;
struct no{int from,to,d;}edge[max_n];
bool operator<(const no &x,const no &y){
return x.d<y.d;
}
int pre[max_n];
int found(int x){
int r=x;
while(r!=pre[r]){
r=pre[r];
}
while(pre[x]!=x){
int t=pre[x];
pre[x]=r;
x=t;
}
return r;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
int T;cin>>T;
while(T--){
int ans=0;
int n,m,r;cin>>n>>m>>r;
for(int i=0;i<n+m;i++) pre[i]=i;
for(int i=1;i<=r;i++){
scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].d);//!!!!!!!
edge[i].d=-edge[i].d;
edge[i].to+=n;
/*edge[i+r].from=edge[i].to;
edge[i+r].to=edge[i].from;
edge[i+r].d=edge[i].d;*/
}
sort(edge+1,edge+1+r);
for(int i=1;i<=r;i++){
int fx=found(edge[i].from),fy=found(edge[i].to);
if(fx!=fy){
pre[fx]=fy;
ans+=edge[i].d;
}
}
cout<<(n+m)*N+ans<<endl;
}
return 0;
}