poj3723Conscription

题目链接:http://poj.org/problem?id=3723

建图时将女生编号都加n(男生数目),求最大生成树。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn=10000+1;
 6 struct node
 7 {
 8     int u,v,w;
 9 };
10 node map[5*maxn];
11 int father[2*maxn];
12 int n,m,r;
13 bool cmp(node x,node y)
14 {
15     return x.w>y.w;
16 }
17 void Init()
18 {
19     for(int i=0;i<n+m;i++)
20         father[i]=i;
21 }
22 int find_set(int x)
23 {
24     if(father[x]==x)
25         return x;
26     else
27         return father[x]=find_set(father[x]);
28 }
29 
30 int kruskal()
31 {
32     int i,sum=0;
33     Init();
34     sort(map,map+r,cmp);
35      for(i=0;i<r;i++)
36    {
37 
38      int x=find_set(map[i].u);
39      int y=find_set(map[i].v);
40      if(x!=y)
41      {
42          father[y]=x;
43          sum+=map[i].w;
44      }
45     }
46    return sum;
47 }
48 int main()
49 {
50    int t,x,y,d;
51    scanf("%d",&t);
52    while(t--)
53    {
54        scanf("%d%d%d",&n,&m,&r);
55        for(int i=0;i<r;i++)
56        {
57             scanf("%d%d%d",&x,&y,&d);
58              map[i].u=x;
59              map[i].v=y+n;
60              map[i].w=d;
61        }
62        printf("%d
",(n+m)*10000-kruskal());
63    }
64     return 0;
65 }
原文地址:https://www.cnblogs.com/yijiull/p/6637852.html