POJ - 2377 Bad Cowtractors Kru最大生成树
Bad Cowtractors
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
* 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
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
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.
#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最大生成树的更多相关文章
- poj 2377 Bad Cowtractors (最大生成树prim)
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- poj 2377 Bad Cowtractors(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- POJ 2377 Bad Cowtractors (Kruskal)
题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...
- POJ 2377 Bad Cowtractors( 最小生成树转化 )
链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- MST:Bad Cowtractors(POJ 2377)
坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...
- poj 2377 最大生成树
#include<stdio.h> #include<stdlib.h> #define N 1100 struct node { int u,v,w; }bian[11000 ...
随机推荐
- Swift 学习笔记 (枚举)
枚举为一种相关值定义了一个通用类型,从而可以让你在代码中类型安全的操作这些值. Swift中的枚举很灵活,不需要给每一个枚举中的成员都提供值.如果一个值(所谓 原时值) 要被提供给每一个枚举成员,那么 ...
- HTML 学习笔记 JQuery(animation)
动画效果也是JQuery库吸引人的地方,通过JQuery的动画方法,能够轻松的为网页天假非常紧菜的视觉效果. show()方法和hide()方法 show()方法和hide()方法是JQuery中最基 ...
- Django框架ORM单表添加表记录_模型层
此方法依赖的表时之前创建的过的一张表 参考链接:https://www.cnblogs.com/apollo1616/p/9840354.html 方法1: # 语法 [变量] = [表名].obje ...
- OpenGLES 与 WebGL 中顶点属性的组织格式的误解 - 一个不好笑的笑话
版权声明:本文为博主原创文章,未经博主同意不得转载.转载联系 QQ 30952589.加好友请注明来意. https://blog.csdn.net/sleks/article/details/289 ...
- Qt — 子窗体操作父窗体中的方法
父窗体与子窗体各自的代码如下: 1. 父窗体的代码: void FartherWindow::addactions() { SubWindow subwindow(this); // 把父窗体本身t ...
- linux下更改文件夹名
mv 旧文件夹名 新文件夹名 mv /usr/bin/python_old /usr/bin/python_new
- Vue:实践学习笔记(5)——Vue-Cli脚手架的使用
Vue:实践学习笔记(5)——Vue-Cli脚手架的使用 快速开始 项目配置 可视化配置 vue ui 命令配置 vue init webpack vue-demo(项目名) 运行测试 进入vue-d ...
- PAT天梯赛 L2-002. 链表去重 【STL】
题目链接 https://www.patest.cn/contests/gplt/L2-002 思路 用结构体 存储 一个结点的地址 值 和下一个地址 然后从首地址开始 往下走 并且每个值的绝对值 都 ...
- 构建工具是如何用 node 操作 html/js/css/md 文件的
构建工具是如何用 node 操作 html/js/css/md 文件的 从本质上来说,html/js/css/md ... 源代码文件都是文本文件,文本文件的内容都是字符串,对文本文件的操作其实就是对 ...
- 20145239杜文超 实验五 Java网络编程
20145239 实验五 Java网络编程 实验内容 组队,一人服务器,一人客户端. 下载加解密代码,先编译运行代码,一人加密一人解密,适当修改代码. 然后集成代码,一人加密后通过TCP发送,加密使用 ...