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 }


  • 相关阅读:
    零基础逆向破解教程之图文!-第五课【剽窃软件】
    零基础逆向破解教程之图文!-第四课【篡改内容】
    零基础逆向破解教程之图文!-第三课【脱壳破解】
    Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库
    多线程数据库查询(ADO)
    delphi通过TADOConnection组件直接连接MSSQL数据库并读写数据。
    Delphi动态配置ODBC数据源--SQL Server版本
    [原创]HTML 用div模拟select下拉框
    [原创]delphi一次性批量在TScrollBox中显示N个复选框TCheckBox的源码
    php缩小png图片时,不损失透明色的办法
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804143.html
Copyright © 2011-2022 走看看