Out of Hay
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11580   Accepted: 4515

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

/*求最小生成树中最大的权值*/
#include<stdio.h>
#include<algorithm>
using namespace std;
int pre[2020];
int ans;
struct st
{
int a,b,l;
}data[10010];
int find(int N)
{
return pre[N]==N?N:pre[N]=find(pre[N]);
}
int cmp(st a,st b)
{
return a.l<b.l;
}
int main()
{
int i,n,m,ans,x,y;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
pre[i]=i;
for(i=1;i<=m;i++)
scanf("%d%d%d",&data[i].a,&data[i].b,&data[i].l);
sort(data+1,data+m+1,cmp);
for(i=1,ans=0;i<=m;i++)
{
x=find(data[i].a);//常常写成x=pre[data[i].a]!!!理解最重要! ! !
y=find(data[i].b);
if(x!=y)
{
if(x>y)
pre[x]=y;
else
pre[y]=x;
ans=max(ans,data[i].l);
}
}
printf("%d\n",ans);
return 0;
}

Out of Hay(poj2395)(并查集)的更多相关文章

  1. 【CF659F】Polycarp and Hay(并查集,bfs)

    题意: 构造一个矩阵,使得: 矩阵所有格子中数字都小于等于原矩阵,并且至少有一个元素和原矩阵相等, 构造的矩阵除了0以外的数字必须联通并且相等,矩阵中元素之和为K. n,m<=1e3,1< ...

  2. 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 ...

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

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  4. codeforces 659F F. Polycarp and Hay(并查集+bfs)

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

  5. POJ 3657 Haybale Guessing(区间染色 并查集)

    Haybale Guessing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2384   Accepted: 645 D ...

  6. poj-3657 Haybale Guessing(二分答案+并查集)

    http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...

  7. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  8. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  9. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  10. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

随机推荐

  1. 做考试系统用到的关于onbeforeunload一些兼容性问题

    最近做考试系统,一些效果存在一些兼容性问题,尤其是ie6,本人js不是很好,通过查资料解决了这些问题,现总结下: 关于onbeforeunload问题: 考试时离开页面需要提示,但是直接用此方法或on ...

  2. 【GISER&&Painter】Chapter02:WebGL中的模型视图变换

    上一节我们提到了如何在一张画布上画一个简单几何图形,通过创建画布,获取WebGLRendering上下文,创建一个简单的着色器,然后将一些顶点数据绑定到gl的Buffer中,最后通过绑定buffer数 ...

  3. 黑马day16 jquery入门

    jquery: 1.jQuery 对象就是通过jQuery包装DOM对象后产生的对象. jQuery对象是jQuery独有的.假设一个对象是jQuery对象,那么它就能够使用jQuery里的方法:$( ...

  4. [Android Studio] Android Studio使用教程(二)

    以下是本次Google I/O大会发布的IDE Android Studio使用教程第二篇: 在Android Studio使用教程(一)中简要介绍了Android Studio的基本使用,包括安装. ...

  5. 一致性hash在分布式系统中的应用

    场景 如果要设计一套KV存储的系统,用户PUT一个key和value,存储到系统中,并且提供用户根据key来GET对应的value.要求随着用户规模变大,系统是可以水平扩展的,主要要解决以下几个问题. ...

  6. Python学习(四)数据结构 —— dict

    词典类型 dict 字典由键(key)和对应值(value)成对组成.字典也被称作关联数组或哈希表. dict 赋值 dict 整体放在花括号{}中,每个键与值用冒号隔开(:),每对用逗号分割: d ...

  7. Meanshift,聚类算法(转)

    原帖地址:http://www.cnblogs.com/liqizhou/archive/2012/05/12/2497220.html   记得刚读研究生的时候,学习的第一个算法就是meanshif ...

  8. FreeBSD与Linux十个本质上的区别

    Linux的标志是一只十分可爱的小企鹅,而FreeBSD的标志是一个拿着叉子的小恶魔.你是否经常会听到人们把 Linux及 BSD 系统混为一谈?是的,我有时会经常听到一些新手,甚至于媒体都这么说.当 ...

  9. DOS命令:列出某目录下的所有文本文件名并重定向到某文件

    命令如下: >dir /b *.txt>output.txt dir无需说,/b 是只要文件名,>是重定向. 2013年11月7日13:36:57

  10. JVM: can not create native thread

    检查系统限制,并根据需要修改过低的值: echo "100000" > /proc/sys/kernel/threads-max echo "100000" ...