Out of Hay

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 13094 Accepted: 5078

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

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.

Source

USACO 2005 March Silver

求最小生成树的最大边,Kruskal

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 11000; int n,m; int pre[3000]; typedef struct node
{
int x;
int y;
int dis;
}K;
K Edge[MAX];
void init()
{
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
}
bool cmp(node a,node b)
{
return a.dis<b.dis;
} int Find(int x)
{
return x==pre[x]?x:pre[x]=Find(pre[x]);
} int Kruskal()
{
int num=0,Max=0;
for(int i=0;i<m;i++)
{
int a=Find(Edge[i].x);
int b=Find(Edge[i].y);
if(a!=b)
{
num++;
pre[a]=b;
if(Max<Edge[i].dis)
{
Max=Edge[i].dis;
}
}
if(num==n-1)
{
break;
}
}
return Max;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&Edge[i].x,&Edge[i].y,&Edge[i].dis);
}
sort(Edge,Edge+m,cmp);
printf("%d\n",Kruskal());
}
return 0;
}

Out of Hay的更多相关文章

  1. 洛谷P1547 Out of Hay

    题目背景 奶牛爱干草 题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,0 ...

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

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

  3. 洛谷P2925 [USACO08DEC]干草出售Hay For Sale

    题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...

  4. [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草

    [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草 试题描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草. 他知道N(1≤N≤100)个干草 ...

  5. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  6. Hay Points

    Hay Points TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 1022   Accepted: 602 Descript ...

  7. BZOJ1606: [Usaco2008 Dec]Hay For Sale 购买干草

    1606: [Usaco2008 Dec]Hay For Sale 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 612  Solved: 46 ...

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

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

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

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

随机推荐

  1. HTML语言的一些元素(四)

    以下资料整理自网路 1.锚点是网页制作中超级链接的一种,又叫命名锚记.命名锚记像一个迅速定位器一样是一种页面内的超级链接,运用相当普遍. 英文名:anchor 使用命名锚记可以在文档中设置标记,这些标 ...

  2. CentOS 7 安装Dukto(局域网通信工具)

    rmp包 http://download.opensuse.org/repositories/home:/colomboem/CentOS_7/x86_64/dukto-6.0-13.1.x86_64 ...

  3. Ruby On Rails经典书籍下载地址

    Web开发敏捷之道-应用Rails进行敏捷Web开发   http://vdisk.weibo.com/s/t47M5Q3WInwc RUBY ON RAILS入门经典   http://downlo ...

  4. Git的status命令

    这个命令是用来查看当前版本库的状态,如果当前版本库没有任何改变,而且都已提交,那么使用status命令结果如下: 如果有文件改变了,但是没有使用add,那么使用status命令结果如下: 如果使用了a ...

  5. SPOJ 220 Relevant Phrases of Annihilation(后缀数组)

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...

  6. ORA-12154的原因

    一个很难想到的引起ORA-12154的原因         使用OracleClient.OracleConnection时(我连的是Oracle 9i,其他版本未知),如果你的执行目录太长或者有括号 ...

  7. mongodb版本管理

    使用gradle. 查找最新版本http://mvnrepository.org/ compile "org.mongeez:mongeez:0.9.6" 配置spring < ...

  8. Silverlight ModelView中调用UI进程

    Silverlihgt:  Deployment.Current.Dispatcher.BeginInvoke wpf: App.Current.Dispatcher.Invoke  

  9. Linux(CentOS) 如何查看当前占用CPU或内存最多的K个进程

    一.可以使用以下命令查使用内存最多的K个进程 方法1: ps -aux | sort -k4nr | head -K 如果是10个进程,K=10,如果是最高的三个,K=3 说明:ps -aux中(a指 ...

  10. Java如何对ArrayList里的元素排序