Bzoj 3390: [usaco DEC] revenge against the bad cowtractors

Source: Internet
Author: User
Question 3390: [usaco2004 DEC] Time Limit: 1 sec memory limit: 128 MB revenge against bad cowtractors
Submit: 53 solved: 37
[Submit] [Status] Description Niu beiqian was hired to build an Internet between N (2 ≤ n ≤ 1000) sheds. she has explored M (1 ≤ m ≤ 20000) lines that can be built. Each line connects two cowsheds and charges C (1 ≤ C ≤ 100000 ). john, the farmer, is very stingy. He hopes that he will not even pay for the construction at least. bessie decided to retaliate when she learned that the work money was about to blow off. she planned to build some lines to connect all the cowshed together, maximizing John's cost. but she cannot create a ring, so John will find out. 1st rows of input: n, m. rows 2nd to m + 1: Three integers, indicating two endpoints and costs of a possible line. maximum output cost. if a reasonable line cannot be built, output-1 sample input5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17 Sample output42

Connect 4, 5, 2, 5, 2, 3, 1, and 3, and spend 17 + 8 + 10 + 7 = 42 Solutions

This topic is the maximum spanning tree! -1 indicates that a tree cannot be formed.

Code
 1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<algorithm>  4 using namespace std; 5 const int Maxn=1000; 6 int father[Maxn+10]; 7 inline void initFather(){ 8     for (int i=1;i<=Maxn;i++) father[i]=i; 9 }10 11 inline int getFather(int x){12     return father[x]=father[x]==x?x:getFather(father[x]);13 }14 15 inline void mergeFather(int x,int y){16     int lx=getFather(x),ly=getFather(y);17     if (lx<ly){18         father[ly]=lx;19     }else{20         father[lx]=ly;21     }22 } 23 24 struct Edge{25     int x,y;26     int w;27     Edge(){}28     Edge(int a,int b,int c){29         x=a;30         y=b;31         w=c;32     }33 };34 35 const int Maxm=20000;36 Edge e[Maxm+10];37 38 bool cmp(Edge a,Edge b){39     if (a.w>b.w) return true;40     return false;41 }42 43 int cnt,ans; 44 45 int n,m;46 int main(){47     scanf("%d%d",&n,&m);48     initFather();49     for (int i=1;i<=m;i++){50         int x,y,z;51         scanf("%d%d%d",&x,&y,&z);52         e[i]=Edge(x,y,z);53     }54     sort(e+1,e+m+1,cmp);55     cnt=n;56     for (int i=1;i<=m;i++){57         int x=e[i].x,y=e[i].y,w=e[i].w;58         if (getFather(x)!=getFather(y)){59             mergeFather(x,y);60             cnt--;61             ans+=w;62         }63         if (cnt==1) break;64     }65     if (cnt!=1){66         printf("-1\n");67         return 0;68     }69     printf("%d\n",ans);70     return 0;71 }
View code

 

Bzoj 3390: [usaco DEC] revenge against the bad cowtractors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.