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. web小流量实验方案

    近期在思考怎么做小流量,在网上搜了一下,总结例如以下: 1.前提,站点pv已经达到了一定的规模,比方上百万pv,不做小流量冒然更新功能,可能会带来大面积流量损失.在这样的前提下须要做小流量实验 2.什 ...

  2. flex创建hashMap

    1,定义hashMap的接口. import flash.events.IEventDispatcher; import mx.events.CollectionEvent; /** *  Dispa ...

  3. C#特性杂谈

    文中充满了各种C#与其他语言的对比及吐槽, 希望介意者勿观… 当然, 鉴于太乱, 我怀疑有没有人能看完. 学习C# Hello World 变量与表达式 动态类型 值类型和引用类型 checked支持 ...

  4. html5.js 让所有IE支持HTML5

    摘自: http://hi.baidu.com/skway/item/33f38a9487356b4ff14215cf?qq-pf-to=pcqq.c2c html5.js 让所有IE支持HTML5 ...

  5. Javaee项目经验须知

    Java的主要应用领域就是企业级的项目开发!具体要点(09年,那一年我去面试,被拒了几次,想起来还不错!他锻炼了我的心理素质,让我体会到很多,笑一个吧!): 1.掌握项目开发的基本步骤 2.具备极强的 ...

  6. Python socket – network programming tutorial

    原文:https://www.binarytides.com/python-socket-programming-tutorial/ --------------------------------- ...

  7. 转:Sublime Text 2 实用快捷键[Mac OS X]

    转: http://lucifr.com/2011/09/10/sublime-text-2-useful-shortcuts/ Sublime Text 2 实用快捷键[Mac OS X] luca ...

  8. QPS、RT、PV、UV之间的关系

    QPS: 每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力. QPS = req/sec = 请求数/秒 QPS统计方式 [一般使用 http_load 进行统计 ...

  9. 理解 select poll epoll

    举例说明:老师收学生作业,相当于应用层调用I/O操作. 1.老师逐个收学生作业,学生没有做完,只能阻塞等待,收了之后,再去收下一个学生的作业.这显然存在性能问题. 2.怎么解决上面的问题? 老师找个班 ...

  10. android DPI与分辨率的关系及计算方式

    android DPI与分辨率的关系及计算方式   Low   density (120), ldpi Medium   density (160), mdpi High   density (240 ...