POJ - 2377 Bad Cowtractors Kru最大生成树

时间:2022-03-27 06:53:23

Bad Cowtractors

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.

 
 
题意:求最大生成树。
思路:只需在生成树基础上用sort降序排序。
 
#include<stdio.h>
#include<algorithm>
using namespace std; int f[]; struct Node{
int u,v,w;
}edge[]; bool cmp(Node a,Node b)
{
return a.w>b.w;
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} int kru(int n,int m)
{
int i;
for(i=;i<=n;i++){
f[i]=i;
}
sort(edge+,edge+m+,cmp);
int cnt=,ans=;
for(i=;i<=m;i++){
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=w;
f[fv]=fu;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
} int main()
{
int n,m,u,v,w,i;
scanf("%d%d",&n,&m);
for(i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
edge[i].u=u;
edge[i].v=v;
edge[i].w=w;
}
printf("%d\n",kru(n,m));
return ;
}

POJ - 2377 Bad Cowtractors Kru最大生成树的更多相关文章

  1. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  2. poj 2377 Bad Cowtractors(最大生成树!)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  3. poj 2377 Bad Cowtractors

    题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...

  4. poj - 2377 Bad Cowtractors&amp&semi;&amp&semi;poj 2395 Out of Hay&lpar;最大生成树&rpar;

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  5. POJ 2377 Bad Cowtractors &lpar;Kruskal&rpar;

    题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...

  6. POJ 2377 Bad Cowtractors( 最小生成树转化 )

    链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...

  7. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  8. MST&colon;Bad Cowtractors&lpar;POJ 2377&rpar;

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

  9. poj 2377 最大生成树

    #include<stdio.h> #include<stdlib.h> #define N 1100 struct node { int u,v,w; }bian[11000 ...

随机推荐

  1. opendaylight的Beryllium安装

    1.首先安装jdk  #sudo apt-get install openjdk-7-jdk 2.安装vim编辑工具   #sudo apt-get install vim 3.编辑~/.bashrc ...

  2. laravel&sol;lumen 单元测试

    Testing Introduction Application Testing Interacting With Your Application Testing JSON APIs Session ...

  3. Redis在WEB开发中的应用与实践

    Redis在WEB开发中的应用与实践 一.Redis概述: Redis是一个功能强大.性能高效的开源数据结构服务器,Redis最典型的应用是NoSQL.但事实上Redis除了作为NoSQL数据库使用之 ...

  4. 《DSP using MATLAB》示例Example5&period;2

    代码: L = 5; N = 20; k = [-N/2:N/2]; % square wave parameters xn = [ones(1,L), zeros(1,N-L)]; % Sq wav ...

  5. Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  6. 【ufldl tutorial】Convolution and Pooling

    卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...

  7. 管理Activity

     开源中国摘取的代码,这个可以管理activity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  8. MD5 SHA1 HMAC HMAC&lowbar;SHA1区别

    MD5.SHA1.HMAC.HMAC_SHA1区别 引言     什么是MD5,什么是SHA1,如何校验这些Hash.还有拿单个apk文件的MD5,SHA1讯问是不是原版的问题,在这里,让我们先来了解 ...

  9. 微信小程序调接口常见问题解决方法

    第一次调接口时遇见的bug. 注意:接口的域名不能使用 IP 地址或 localhost,且不能带端口号: 微信小程序如何调接口? wx.request({ url: 'http://miniapp/ ...

  10. SQL2005中的事务与锁定(九)-(2)- 转载

    -------------------------------------------------------------------------- Author : HappyFlyStone -- ...