Conscription(Kruskal)
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
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 < 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
POJ Monthly Contest – 2009.04.05, windy7926778 大概題意就是征男女兵,然後一個兵就需要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)