POJ:2395-Out of Hay
Out of Hay
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18780 Accepted: 7414
Description
The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She’ll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1.
Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she’s only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.
Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she’ll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she’ll have to traverse.
Input
Line 1: Two space-separated integers, N and M.
Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
Output
- Line 1: A single integer that is the length of the longest road required to be traversed.
Samle Input
3 3
1 2 23
2 3 1000
1 3 43
Sample Output
43
Hint
OUTPUT DETAILS:
In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.
解题心得:
- 题意就是有n个农场,m条边,要选总长度最小的边将n个农场都连接起来,并且要求最长的边最小,其实很简单啊,直接用Kruskal跑最小生成树,最后一定是满足总长度最小最大的边最小的。
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e4+100;
struct Path{
int s,e,len;
bool operator < (const Path &a) const {
return a.len > len;
}
}path[maxn];
int n,m,father[maxn];
void init() {
for(int i=1;i<=n;i++)
father[i] = i;
for(int i=0;i<m;i++)
scanf("%d%d%d",&path[i].s,&path[i].e,&path[i].len);
sort(path,path+m);
}
int find(int x) {
if(father[x] == x)
return x;
return father[x] = find(father[x]);
}
void merge(int x,int y) {
int fx = find(x);
int fy = find(y);
father[fx] = fy;
}
int main() {
scanf("%d%d",&n,&m);
init();
int Max = -1;
for(int i=0;i<m;i++) {
if(find(path[i].e) != find(path[i].s)) {
merge(path[i].s , path[i].e);
Max = path[i].len;
}
}
printf("%d",Max);
return 0;
}
POJ:2395-Out of Hay的更多相关文章
- 瓶颈生成树与最小生成树 POJ 2395 Out of Hay
百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...
- POJ 2395 Out of Hay(最小生成树中的最大长度)
POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...
- poj:4091:The Closest M Points
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- POJ 2395 Out of Hay(MST)
[题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...
- poj 2395 Out of Hay(最小生成树,水)
Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...
- POJ 2395 Out of Hay (prim)
题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...
- POJ 2395 Out of Hay 草荒 (MST,Kruscal,最小瓶颈树)
题意:Bessie要从牧场1到达各大牧场去,他从不关心他要走多远,他只关心他的水袋够不够水,他可以在任意牧场补给水,问他走完各大牧场,最多的一次需要多少带多少单位的水? 思路:其实就是要让所带的水尽量 ...
- POJ 2395 Out of Hay (Kruskal)
题意:从待选的路里面选出若干将所有点连通,求选出的边里最长边的最小值. 算法:要使得树的最长边最小,那么每次确定的边都应是待选边里最小的,即最小生成树.对应Kruskal算法. #include &l ...
随机推荐
- InvocationTargetException异常
package com.smbea.demo.reflect; /** * 越界异常 * @author hapday * @date 2017年1月20日 @time下午7:59:01 */ pub ...
- [转]QT 4.8 静态库编译方法
最最初踏上QT之路是受到了XiaomaGee的指点,相比于常规的窗口程序开发,QT有着以下特点: 1. 优良的跨平台特性(支持Win.Linux.Mac 不同的平台下只需重新编译即可使用) 2. 面向 ...
- formvalidator插件
一.引用jquery 二.引用formValidator.js //================================================================== ...
- Struts2_简单数据验证
在Action 中添加 FieldError if(name == null || !name.equals("admin")){ this.addFieldError(" ...
- day003-List类、Set类
(一) 知识回顾1. day002总结 泛型没有多态.如果泛型能实现多态,那么数据类型就不安全了.违背了设计泛型的初衷. 1.1 伪泛型 泛型只存在编译器,编译生成的字节码中,不存在泛型变量的. 1. ...
- php中的curl_multi的应用(php多进程)
相信许多人对PHP手册中语焉不详的curl_multi一族的函数头疼不已,它们文档少,给的例子 更是简单的让你无从借鉴,我也曾经找了许多网页,都没见一个完整的应用例子. curl_multi_add_ ...
- mysql:数据库保存时间的类型——int和datetime的区别
我们都知道,时间保存在数据库中,可以选择使用两种类型,一种是int,一种是datetime 那么,它们两个有什么区别呢?要怎么用呢? 现在和小仓鼠一起来探讨一下 1.int和datetime的使用区别 ...
- BZOJ 3090: Coci2009 [podjela]
3090: Coci2009 [podjela] Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 23 Solved: 17[Submit][Statu ...
- CRM WebClient UI里的文件是如何上传到Netweaver后台的
使用Chrome开发者工具调试CRM WebClient UI里附件上传的功能: 从本地选择一个文件,断点触发: 前端取得用户选中上传的文件名: Jerry.txt 点Attach按钮后,触发ABAP ...
- mysql索引长度
http://blog.csdn.net/qsc0624/article/details/51335632 大家应该知道InnoDB单列索引长度不能超过767bytes,联合索引还有一个限制是长度不能 ...