首页 > 代码库 > (最大生成树) poj 3727
(最大生成树) poj 3727
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8330 | Accepted: 2894 |
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 boyy 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 < 10000
Output
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
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<string>using namespace std;int fa[100010],n,m,r;struct node{ int x,y,w;}e[100010];bool cmp(node a,node b){ return a.w>b.w;}int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);}int main(){ int tt; scanf("%d",&tt); while(tt--) { int ans=0; scanf("%d%d%d",&n,&m,&r); for(int i=0;i<=n+m;i++) fa[i]=i; for(int i=1;i<=r;i++) { scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w); e[i].y+=n; } sort(e+1,e+1+r,cmp); for(int i=1;i<=r;i++) { int fx,fy; fx=find(e[i].x),fy=find(e[i].y); if(fx!=fy) { fa[fx]=fy; ans+=e[i].w; } } printf("%d\n",(n+m)*10000-ans); } return 0;}
(最大生成树) poj 3727