1682: [Usaco2005 Mar]Out of Hay 干草危机
1682: [Usaco2005 Mar]Out of Hay 干草危机
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 391 Solved: 258
[Submit][Status]
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.
Sample Input
1 2 23
2 3 1000
1 3 43
Sample Output
由1到达2,需要经过长度23的道路;回到1再到3,通过长度43的道路.最长道路为43
HINT
Source
题解:既然题目说了所有点均与点1联通(phile:废话,那不就是联通无向图啊),那么显(读xian2,我们数学老师口头禅)然这个问题成了最小生成树,然后只要求出最小生成树最大边的值就Accept啦。。
var
i,j,k,l,m,n:longint;
c:array[..] of longint;
a:array[..,..] of longint;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;
x:=a[(l+r) div ,];
repeat
while a[i,]<x do inc(i);
while a[j,]>x do dec(j);
if i<=j then
begin
swap(a[i,],a[j,]);
swap(a[i,],a[j,]);
swap(a[i,],a[j,]);
inc(i);dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
function getfat(x:longint):longint;
begin
while x<>c[x] do x:=c[x];
getfat:=x;
end;
function tog(x,y:longint):boolean;
begin
exit(getfat(x)=getfat(y));
end;
procedure merge(x,y:longint);
begin
c[getfat(x)]:=getfat(y);
end;
begin
readln(n,m);
for i:= to m do
readln(a[i,],a[i,],a[i,]);
for i:= to n do c[i]:=i;
sort(,m);
j:=;
l:=;
for i:= to n- do
begin
while tog(a[j,],a[j,]) do inc(j);
if a[j,]>l then l:=a[j,];
merge(a[j,],a[j,]);
end;
writeln(l);
end.
1682: [Usaco2005 Mar]Out of Hay 干草危机的更多相关文章
- 【BZOJ】1682: [Usaco2005 Mar]Out of Hay 干草危机(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1682 最小生成树裸题.. #include <cstdio> #include < ...
- BZOJ 1682: [Usaco2005 Mar]Out of Hay 干草危机
Description 牛们干草要用完了!贝茜打算去勘查灾情. 有N(2≤N≤2000)个农场,M(≤M≤10000)条双向道路连接着它们,长度不超过10^9.每一个农场均与农场1连通.贝茜要走遍每一 ...
- bzoj 1682: [Usaco2005 Mar]Out of Hay 干草危机【并查集+二分】
二分答案,把边权小于mid的边的两端点都并起来,看最后是否只剩一个联通块 #include<iostream> #include<cstdio> using namespace ...
- bzoj1682[Usaco2005 Mar]Out of Hay 干草危机*
bzoj1682[Usaco2005 Mar]Out of Hay 干草危机 题意: 给个图,每个节点都和1联通,奶牛要从1到每个节点(可以走回头路),希望经过的最长边最短. 题解: 求最小生成树即可 ...
- [Usaco2005 Mar]Out of Hay 干草危机
题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,000)条双向道路,所有 ...
- 【最小生成树】BZOJ1682[Usaco2005 Mar]-Out of Hay 干草危机
...最小生成树裸题,9月最后一天刷水刷水. #include<iostream> #include<cstdio> #include<algorithm> usi ...
- BZOJ 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
题目 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机 Time Limit: 5 Sec Memory Limit: 64 MB Desc ...
- 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: ...
- BZOJ1680: [Usaco2005 Mar]Yogurt factory
1680: [Usaco2005 Mar]Yogurt factory Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 106 Solved: 74[Su ...
随机推荐
- GridView等表格模板列绑定数据的方法
//绑定GridView每一行中的CheckBoxList protected void GridView1_RowDataBound(object sender, GridViewRowEventA ...
- EasyUI datagrid 删除的时候无法正确找到重复记录row index的问题
EasyUI datagrid 删除的时候无法正确找到重复记录row index的问题: 如果无法正确找到重复记录的row index,就需要进行注意添加的时候所进行的 操作:如果添加的时候就是添加的 ...
- LinuxMint18配置Grub2默认启动操作系统
---恢复内容开始--- 之前电脑里面装了太多系统太乱了,刚好假期回家有一些空闲时间于是开始了重装计划. 现在重新弄好了,有两个系统,一个是Windows10,另一个是LinuxMint18,但是我平 ...
- Bootstrap入门(十六)组件10:well和具有响应式特性的嵌入内容
Bootstrap入门(十六)组件10:well和具有响应式特性的嵌入内容 well组件可以为内容增添一种切入效果. 具有响应式特性的嵌入内容可以根据被嵌入内容的外部容器的宽度,自动创建一个固定的比例 ...
- 关于Visual Studio未能加载各种Package包的解决
参考微软社区的一个答复解决了VS2013的问题: 进入VS对应的用户缓存文件夹,删掉那个Microsoft.VisualStudio.Default.cache缓存文件,就可以了. 这个错误估计是我们 ...
- mac下sublime 配置查看源码
sublime及package control的安装查看之前一篇文章:http://www.cnblogs.com/xdwa/p/5805101.html 安装ctags 安装完毕package co ...
- Hadoop权威指南:从Hadoop URL读取数据
[TOC] Hadoop权威指南:从Hadoop URL读取数据 使用java.net.URL对象从Hadoop文件系统读取文件 实现类似linux中cat命令的程序 文件名 HDFSCat.java ...
- datagridview数据绑定操作数据库实现增删改查
DataSet数据集,数据缓存在客户端内存中,支持断开式连接.DataGridView控件绑定DataSet时,它自动的改变的DS的行的状态,而且在做增删改查的时候,可以借助SqlCommandBui ...
- javascript运行机制详解: 再谈Event Loop(转)
作者: 阮一峰 日期: 2014年10月 8日 一年前,我写了一篇<什么是 Event Loop?>,谈了我对Event Loop的理解. 上个月,我偶然看到了Philip Roberts ...
- Java-8ATM
源代码: import java.util.Scanner;interface fangfa{ public void show();public void qukuan();public void ...