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. MariaDB主从复制搭建

    我的github 安装MySQL服务器 安装数据库 yum install -y mariadb-server 初始化数据库 mysql_secure_installation #MySql初始化脚本 ...

  2. 51nod 1289 大鱼吃小鱼

    #include<bits/stdc++.h> using namespace std; ; int a[maxn],b[maxn]; stack<int>s; int mai ...

  3. Python学习札记(三十四) 面向对象编程 Object Oriented Program 5

    参考:获取对象信息 NOTE 1.type()函数可以用来判断对象的类型: >>> type(123) <class 'int'> >>> type(' ...

  4. IDEA 搭建授权服务器

    引用地址  http://blog.lanyus.com/archives/317.html 今天突出发现基本公开的激活地址都被屏蔽了(IDEA 2017.3 版本) 记录一下 docker pull ...

  5. Python中通过多个字符分割(split)字符串的方法--转载

    Python中字符串自带的split方法一次只能使用一个字符对字符串进行分割,但是python的正则模块则可以实现多个字符分割 import re re.split('_#|','this_is#a| ...

  6. php环境重启

    php-fpm mysql nginx 一个lnmp的服务,主要就是靠这三个来维持的. 重启nginx # /usr/local/nginx/sbin/nginx -s stop # /usr/loc ...

  7. Educational Codeforces Round 23C

    超级坑的水题!!!想了两天没一点思路,看了题解第一段话就做出来了 刚开始一直在想找到通项就是例如an*10^n+...+a0*10^0-an-...-a0>=s,然后从这个里面找到规律,结果走进 ...

  8. 素数对猜想之python3实现

    题目 让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想”认为“存在无穷多对相邻且差为 ...

  9. Python3 列表List(十一)

    list是一种有序可重复的集合,可以随时添加和删除其中的元素. 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. ...

  10. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 03

    作业要求与 [https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 相同 版本控制:https://git.coding.net/li ...