码迷,mamicode.com
首页 > 其他好文 > 详细

Bad Cowtractors POJ - 2377

时间:2017-08-11 23:48:18      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:each   ensure   decide   nts   air   net   log   return   install   

Bessie has been hired to build a cheap internet network among Farmer John‘s N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn‘t even want to pay Bessie.

Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".Input

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.

Output

* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

Sample Input

5 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 Output

42

Hint

OUTPUT DETAILS:

The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
题解:把边权取反之后用最小生成树
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn=20005;
 8 
 9 struct edge{
10     int u,v,cost;
11     bool operator<(const edge& i)const{
12         return cost>i.cost;
13     }
14 }es[maxn];
15 
16 int n,m,cnt;
17 int F[1005];
18 
19 int Find(int a){
20     if(a!=F[a]) F[a]=Find(F[a]);
21     return F[a];
22 }
23 
24 bool unite(int a,int b){
25     int x=Find(a),y=Find(b);
26     if(x==y) return false;
27     else { F[x]=y; return true; } 
28 }
29 
30 int Kruskal(){
31     sort(es,es+m);
32     int ans=0;
33     for(int i=0;i<m;i++) if(unite(es[i].u,es[i].v)) { ans=ans+es[i].cost; cnt++; } 
34     return ans;
35 }
36 
37 int main()
38 {    while(~scanf("%d%d",&n,&m)){
39         for(int i=0;i<m;i++) scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);
40         for(int i=1;i<=n;i++) F[i]=i;
41         cnt=0;
42         int ans=Kruskal();
43         if(cnt!=n-1) cout<<"-1"<<endl;
44         else cout<<ans<<endl;
45     
46         /*
47         int x,y,most;
48         for(int i=0;i<m;i++){
49             scanf("%d%d%d",&x,&y,&most);
50             es[i]=(edge){x,y,-most}; 
51         }
52         bool flag=true;
53         for(int i=1;i<=n;i++) F[i]=i;
54         int ans=Kruskal();
55         for(int i=2;i<=n;i++) if(F[i]!=F[1]) flag=false;
56         if(flag) cout<<ans<<endl;
57         else cout<<"-1"<<endl;*/
58     }
59     return 0;
60 }

 

Bad Cowtractors POJ - 2377

标签:each   ensure   decide   nts   air   net   log   return   install   

原文地址:http://www.cnblogs.com/zgglj-com/p/7348301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!