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. 《Linux兵书》

    <Linux兵书> 基本信息 作者: 刘丽霞    杨宇 丛书名: 程序员藏经阁 出版社:电子工业出版社 ISBN:9787121219924 上架时间:2014-1-13 出版日期:20 ...

  2. Axure Beta 7.0 汉化版下载

    Axure的最新版本下载地址如下 安装文件地址: PC版下载地址:点我下载  Mac版下载地址:点我下载 下载后下载汉化包即可进行汉化(含中文部件库) 汉化文件:点我下载 -------------- ...

  3. 数学图形(2.26) 3D曲线结

    我收集的几种曲线结 knot(huit) #http://www.mathcurve.com/courbes3d/noeuds/noeudenhuit.shtml vertices = 1000 t ...

  4. QT开发(十二)——QT事件处理机制

    一.QT事件简介 QT程序是事件驱动的, 程序的每个动作都是由内部某个事件所触发.QT事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. 常见的QT事件类型如下: 键盘事件: 按键按下和松开 ...

  5. Informatica 常用组件Source Qualifier之一 概述

     转换类型:主动.已连接 1 Source Qualifier 概述 当你添加关系表或平面文件源定义至映射时,需要将它连接至 Source Qualifier 组件.Source Qualifier ...

  6. 阿里云 Ubuntu 14.04 安装mysql 5.6

    1. 升级apt-get apt-get update 如果出现 : 说明没有你不是root用户,则需要用sudo命令 sudo apt-get update 下面出现权限问题都可以参照这个方法. 2 ...

  7. 《java 语言程序设计》第2章编程练习

    2.1 public class test { public static void main(String[] args) { Scanner input = new Scanner(System. ...

  8. HYSBZ 1012 最大数maxnumber

    思路:在单调队列不更新列首.由于查询区间大小不确定,所以不能保证下次是否还用到它 #include <cstdio> #include <cstring> #include & ...

  9. 在centOS上安装VNC

    步骤如下: 1.搜寻VNC Server [root@msg45 wasliberty]# yum search tigervnc-serverLoaded plugins: fastestmirro ...

  10. (笔试题)N!的三进制数尾部0的个数

    题目: 用十进制计算30!(30的阶乘),将结果转换成3进制进行表示的话,该进制下的结果末尾会有____个0. 思路: 这道题与上一篇博文N!尾部连续0的个数的思路是一样的. 计算N!下三进制结果末尾 ...