zoukankan      html  css  js  c++  java
  • POJ 2377

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

    Time Limit: 1000MS Memory Limit: 65536K

    Description

    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.

    1A水过,kruskal

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int n,m;
     5 struct node{
     6     int u,v,c;
     7 }edge[20000+5];
     8 bool cmp(node a,node b){return a.c>b.c;}
     9 int par[1005],rank[1005];
    10 void init()
    11 {
    12     for(int i=1;i<=n;i++)
    13     {
    14         par[i]=i;
    15         rank[i]=0; 
    16     }
    17 }
    18 int find(int x){return( par[x]==x ? x : par[x]=find(par[x]) );} 
    19 void unite(int x,int y)  
    20 {  
    21     x=find(x),y=find(y);  
    22     if(x == y) return;  
    23     if(rank[x] < rank[y]) par[x]=y;  
    24     else  
    25     {  
    26         par[y]=x;  
    27         if(rank[x] == rank[y]) rank[x]++;  
    28     }  
    29 }
    30 int kruskal()
    31 {
    32     int ans=0,cnt=0;
    33     init();
    34     for(int i=1;i<=m;i++)
    35     {
    36         int x=find(edge[i].u) , y=find(edge[i].v);
    37         if(x!=y)
    38         {
    39             unite(x,y);
    40             ans+=edge[i].c;
    41             if(++cnt == n-1) break;
    42         }
    43     }
    44     if(cnt == n-1) return ans;
    45     else return -1;
    46 }
    47 int main()
    48 {
    49     scanf("%d%d",&n,&m);
    50     for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
    51     sort(edge+1,edge+m+1,cmp);
    52     printf("%d
    ",kruskal());
    53 }


  • 相关阅读:
    解决PKIX:unable to find valid certification path to requested target 的问题
    Linux 上的常用文件传输方式介绍与比较
    用VNC远程图形化连接Linux桌面的配置方法
    红帽中出现”This system is not registered with RHN”的解决方案
    linux安装时出现your cpu does not support long mode的解决方法
    CentOS SSH配置
    es6扩展运算符及rest运算符总结
    es6解构赋值总结
    tortoisegit安装、clon、推送
    es6环境搭建
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804143.html
Copyright © 2011-2022 走看看