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. pickle & cPickle ValueError: unsupported pickle protocol: 3

    pickle and cPickle pickle和cPickle是python对象的转储文件,保存的是python对象 他们分别是python2和python3的对应部分,建议引入的时候采用以下方法 ...

  2. POJ Stockbroker Grapevine(floyd)

    https://vjudge.net/problem/POJ-1125 题意: 题意不是很好理解,首先输入一个n,表示有n个股票经纪人,接下来输入n行,每行第一个数m为该股票经纪人认识的经纪人数,然后 ...

  3. SSM(Spring+SpringMVC+Mybatis)+Mysql 框架整合搭建流程以及其间注意事项

    复习SSM框架,太久没用自己手动撘一个,发现自己还是有很多地方忘记了和没注意的事项... 首先,直接给出总流程: 零.引jar包 1.引包(或者写maven.pom) 一.数据库部分 设计数据库各表结 ...

  4. 智课雅思词汇---二十七、形容词后缀-ant/-ent

    智课雅思词汇---二十七.形容词后缀-ant/-ent 一.总结 一句话总结: ...的 后缀:-ant ①[形容词后缀] 大部分与-ance或-ancy,相对应,表示属于...的.具有...性质的 ...

  5. vue双向数据绑定最最最最最简单直观的例子

    vue双向数据绑定最最最最最简单直观的例子 一.总结 一句话总结:双向绑定既不仅model可以影响view的数据,view也可以影响model的数据 view model 数据 1.vue双向数据绑定 ...

  6. Java实现GUI计算器【代码】

    这几天用java写了一个计算器程序,开始写的时候原本只是想实现一下GUI(这个是直接读三个字母还是什么“固椅”的发音)界面,熟悉一下Java GUI 编程,为Java期末大作业练练手,本以为代码不会很 ...

  7. Android手机无线adb

    1.首先电脑,手机通过数据线链接电脑,然后通过adb devices 查看到已连接 2.输入:adb tcpip 5555 3.输入:adb connect 222.222.221.137:5555 ...

  8. Constructor vs Object

    1. Constructor:是用于创建和初始化类中创建的一个对象的一种特殊方法. constructor([arguments]) { ... } 在一个类中只能有一个名为 “constructor ...

  9. Entity Framework 数据并发访问错误原因分析与系统架构优化

    博客地址 http://blog.csdn.net/foxdave 本文主要记录近两天针对项目发生的数据访问问题的分析研究过程与系统架构优化,我喜欢说通俗的白话,高手轻拍 1. 发现问题 系统新模块上 ...

  10. HBase查询优化——持续更新

    Scan:setBatch,setCaching,setCacheBlocks public void setBatch(int batch) public void setCaching(int c ...