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.


解题心得:

  1. 题意就是有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的更多相关文章

  1. 瓶颈生成树与最小生成树 POJ 2395 Out of Hay

    百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...

  2. POJ 2395 Out of Hay(最小生成树中的最大长度)

    POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...

  3. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

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

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

  5. POJ 2395 Out of Hay(MST)

    [题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...

  6. poj 2395 Out of Hay(最小生成树,水)

    Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...

  7. POJ 2395 Out of Hay (prim)

    题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...

  8. POJ 2395 Out of Hay 草荒 (MST,Kruscal,最小瓶颈树)

    题意:Bessie要从牧场1到达各大牧场去,他从不关心他要走多远,他只关心他的水袋够不够水,他可以在任意牧场补给水,问他走完各大牧场,最多的一次需要多少带多少单位的水? 思路:其实就是要让所带的水尽量 ...

  9. POJ 2395 Out of Hay (Kruskal)

    题意:从待选的路里面选出若干将所有点连通,求选出的边里最长边的最小值. 算法:要使得树的最长边最小,那么每次确定的边都应是待选边里最小的,即最小生成树.对应Kruskal算法. #include &l ...

随机推荐

  1. vue打包后CSS中引用的背景图片不显示问题

    vue项目中,在css样式中引用了一张背景图片,开发环境下是可以正常显示,build之后背景图片不显示. 解决方法: 找到build/utils.js文件 修改成为如下所示内容:  添加红框中的内容即 ...

  2. Android 设置软键盘搜索键以及监听搜索键点击事件

    如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮.调用的实现只需要在XML在输入框中加入android:imeOptions="actionSearch" ...

  3. 【Unity3D学习笔记】解决放大后场景消失不显示问题

    不知道为啥,我的Unity场景放大到一定大小后,就会消失... 解决方案: 选中一个GameObject,然后按F键. F键作用是聚焦,视图将移动,以选中对象为中心.

  4. notepad++ TextFX替代

    notepad++目前的版本已没有了TextFX插件,插件的原作者在2008年的时候已停止维护.目前官方的意思是用以下插件替代,见 http://docs.notepad-plus-plus.org/ ...

  5. 永恒之蓝EternalBlue复现

    0x01 漏洞原理:http://blogs.360.cn/blog/nsa-eternalblue-smb/ 目前已知受影响的 Windows 版本包括但不限于:Windows NT,Windows ...

  6. April 16 2017 Week 16 Sunday

    Happiness is a way station between too much and too little. 幸福就是刚刚好. I don't want to talk about it a ...

  7. log4net 简单配置

    <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigu ...

  8. 神奇的暴力数据结构——ODT

    前言 \(ODT\),即珂朵莉树,又称老司机树(\(Old\ Driver\ Tree\)). 它是一个十分暴力的数据结构,可以用于各种乱搞,也非常的实用. 当然,这全要基于一个基本条件:数据随机. ...

  9. 【LOJ6045】「雅礼集训 2017 Day8」价(网络流)

    点此看题面 大致题意: 有\(n\)种药,每种药有一个权值,且使用了若干种药材.让你选择若干种药,使得药的数量与所使用的药材并集大小相等,求最小权值总和. 网络流 \(hl666\):这种数据范围,一 ...

  10. 2017.10.28 针对Java Web应用中错误异常处理方法的运用

    针对Java Web应用中错误异常处理方法的运用 在javaweb中其异常都需要对Checked Exception之下的Exception进行继承,并且有选择地对发生的错误和异常进行处理.Java同 ...