Bad Cowtractors

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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
<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.
 
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
 #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)的更多相关文章

  1. poj 2377 Bad Cowtractors

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

  2. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

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

  3. POJ - 2377 Bad Cowtractors Kru最大生成树

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

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

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

  5. POJ 2377 Bad Cowtractors (Kruskal)

    题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-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. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  9. MST:Bad Cowtractors(POJ 2377)

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

随机推荐

  1. 试着用React写项目-利用styled-components解决样式问题

    转载请注明出处:王亟亟的大牛之路 啰嗦之前先安利,会渐渐丰富前端的知识点 https://github.com/ddwhan0123/Useful-Open-Source-Android 昨天用web ...

  2. CF_863_F(Netflow)

    codeforces_863_F 题目大意:给出一个数组的大小(n<=50),以及每个位置填数的范围限制(若无限制,即为1-n),最后求填出数组的最小花费,定义总花费为数组中每个数出现次数的平方 ...

  3. python 列表嵌套

    >>> a = ['a', 'b', 'c'] >>> n = [, , ] >>> x = [a, n] >>> x [[, ...

  4. python 正则匹配字符串里面的字符

    import re x=re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') print(x)

  5. python 计算字典value值的和

    my_dict = {,,} print(sum(my_dict.values()))

  6. [原][译][osgearth]API加载地球(OE官方文档翻译)

    原文参考:http://docs.osgearth.org/en/latest/developer/maps.html#programmatic-map-creation 本人翻译水平有限... 加载 ...

  7. css3 属性——calc()

    其实在之前学习CSS3的时候,我并没有注意到有calc()这个属性,后来在看一个大牛的代码的时候看到了这个,然后就引发了后来的一系列的查找.学习,以及这篇博客的诞生.好了,废话不多说了,来干正事. 一 ...

  8. 【Demo】Tree.js实例

    Three.js是通过对WebGL接口的封装与简化而形成的一个易用的图形库. 简单点的说法:WebGL可以看成是浏览器给我们提供的接口,在javascript中可以直接用这些API进行3D图形的绘制: ...

  9. 自定义jQuery的animate动画

    //擦除效果 jQuery.extend(jQuery.easing, { easeOutBack : function(x, t, b, c, d, s) { s = s || 1.3; retur ...

  10. 【git】提交到github不显示贡献小绿点问题的解决

    问题描述: 最近一直在用github来写博客,但是今天发现github上的contributions记录并没有我的提交记录. 经过一番百度和自行捣鼓发现了问题所在. 原因: 最近实习,公司给配电脑.原 ...