poj 2377 Bad Cowtractors (最大生成树prim)
Bad Cowtractors
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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".
<br> <br>* 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.
most expensive tree connecting all the barns. If it is not possible to connect
all the barns, output -1.
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17
#include <iostream>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[][];
int dis[];
bool vis[];
int n, m;
void Prime()
{
for (int i = ; i <= n; i++)
{
vis[i] = false;
dis[i] = a[][i];
}
dis[] = ;
vis[] = true;
int ans = ;
for (int i = ; i <= n; i++)
{
int minn = ;
int p = -;
for (int j = ; j <= n; j++)
{
if (!vis[j] && dis[j]>minn)// 是大于,找出最大的边
minn = dis[p = j];
}
if (p == -)
{
cout << "-1" << endl;
return;
}
vis[p] = true;
ans += minn;
for (int j = ; j <= n; j++)
{
if (!vis[j] && dis[j]<a[p][j])//尽可能让边变大
dis[j] = a[p][j];
}
}
cout << ans << endl;
}
int main()
{
while (cin >> n >> m)
{
//初始化为0
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
a[i][j] = ;
}
int x, y, z;
while (m--)
{
scanf("%d%d%d", &x, &y, &z);
if (z>a[x][y])
a[x][y] = a[y][x] = z;
}
Prime();
}
return ;
}
poj 2377 Bad Cowtractors (最大生成树prim)的更多相关文章
- 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 Kru最大生成树
Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...
- 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 (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 ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- MST:Bad Cowtractors(POJ 2377)
坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...
随机推荐
- JavaScript常见算法——去重
刚才看到一篇博文,数组去重的,我先试着写一下:新建一个空数组,对原数组进行for循环,对新数组使用indexOf方法判断新数组中是否有该数组元素,没有的话就加入新数组.后来看文中使用的是HashTab ...
- CycleGAN 配置及其实现
目录 pytorch-CycleGAN-and-pix2pix 环境要求 安装 Train 用已有数据集训练 Test 预训练模型 训练与测试自己的数据集 遇到的问题 Reference pytorc ...
- classloader的演进
classloader从1.6到1.7整体分成了两个版本.重点区别就是并行类加载. 1.6版本 protected synchronized Class loadClass(String name, ...
- ajax方法携带授权标识
$.ajax({ type: "post", url: "/api/login", data: { username: getusername, passwor ...
- mybatis的xml配置中if text判断不为0
问题:money_search_end != ‘0’为0时依然继续执行 解决方案: 1.单个字符要用''.toString() 2.用双引号“”,当然text的引号要改为单引号‘’ <if te ...
- UVA-12657 Boxes in a Line (双向链表)
题目大意:一个1~n的升序数字序列,有4种操作.操作1,将x放到y前面一个位置:操作2将x放到y后面的一个位置:操作3交换x和y的位置:操作4反转整个序列.求经过m次操作后的所有奇数项的和. 题目分析 ...
- APP的六种loading加载样式,全在这...
今天这篇文章是给大家分享的loading加载的设计,文章里面会有一些实例在这分享给大家! 大多数App都要与服务器进行数据的交换,App向服务器发出数据请求,服务器接收到请求之后向App传输相应数据, ...
- 利用Sonar定制自定义JS扫描规则(一)——sonar环境搭建
接触sonar已经有段时间了,最早是在一个项目组里面听到这个框架,后来在公司持续集成活动的推动下,也逐渐学习了sonar的具体功能.sonar集成了findbugs,pmd,checkstyle各种静 ...
- Beta阶段第2周/共2周 Scrum立会报告+燃尽图 14
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...
- ubuntu14.04安装Android Studio出现error while loading shared libraries: libz.so.1的解决方法
参考博客地址: http://blog.csdn.net/newairzhang/article/details/28656693 安装lib32z1就可以解决,如下: 首先,sudo apt-get ...