zoukankan      html  css  js  c++  java
  • Bad Cowtractors POJ

    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 }
  • 相关阅读:
    [CTF隐写]png中CRC检验错误的分析
    Bugku
    Bugku
    【CTF 攻略】CTF比赛中关于zip的总结
    sqlserver中利用Tran_sql把逗号分隔的字符串拆成临时表
    H5摇一摇遇到的问题
    C# MVC 微信支付之微信模板消息推送
    各种大型网站技术架构
    ORM框架详解
    显示实现接口
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7348301.html
Copyright © 2011-2022 走看看