poj3723Conscription
生活随笔
收集整理的這篇文章主要介紹了
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",(n+m)*10000-kruskal()); 63 } 64 return 0; 65 }?
轉載于:https://www.cnblogs.com/yijiull/p/6637852.html
總結
以上是生活随笔為你收集整理的poj3723Conscription的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Adobe Premiere Pro 如
- 下一篇: 错误记录--更改tomcat端口号方法,