Out of Hay(poj2395)(并查集)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11580 | Accepted: 4515 |
Description
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
* 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
Sample Input
3 3
1 2 23
2 3 1000
1 3 43
Sample Output
43
Hint
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)(并查集)的更多相关文章
- 【CF659F】Polycarp and Hay(并查集,bfs)
题意: 构造一个矩阵,使得: 矩阵所有格子中数字都小于等于原矩阵,并且至少有一个元素和原矩阵相等, 构造的矩阵除了0以外的数字必须联通并且相等,矩阵中元素之和为K. n,m<=1e3,1< ...
- 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 ...
- 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 ...
- 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 ...
- POJ 3657 Haybale Guessing(区间染色 并查集)
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2384 Accepted: 645 D ...
- poj-3657 Haybale Guessing(二分答案+并查集)
http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
随机推荐
- 2013年,移动App设计的13大精髓
摘要:在 过去的一年里,移动成主流也让众多的移动应用如雨后春笋般层出不穷,在众多开发者从中获利的同时竞争也愈演愈烈,如何才能保证自己立于不败之地?用户是上 帝,一切还得从应用说起.本文总结了新一年里A ...
- EventBus (二) 使用详解——EventBus使用进阶
相关文章: 1.<EventBus使用详解(一)——初步使用EventBus> 2.<EventBus使用详解(二)——EventBus使用进阶> 一.概述 前一篇给大家装简单 ...
- slf4j log4j logback log4j2关系详解和相关用法
来源:slf4j log4j logback关系详解和相关用法https://www.cnblogs.com/Sinte-Beuve/p/5758971.html The Simple Logging ...
- dedecms织梦 v5.6 两处跨站漏洞
漏洞版本: dedecms织梦 v5.6 漏洞描述: DedeCMS内容管理系统软件采用XML名字空间风格核心模板:模板全部使用文件形式保存,对用户设计模板.网站升级转移均提供很大的便利,健壮的模板标 ...
- 基于cookie或session的登陆验证之安全性问题
因为session是关了浏览器就没了.所以可以通过cookie结合session方法来做验证! 第一次登陆,生成一个cookie,保存一些加密的帐号信息,然后再生成一个session 这样去其他需要验 ...
- 深入理解java虚拟机笔记之一
Java的技术体系主要有支撑java程序运行的虚拟机,提供各开发领域接口支持Java API,java编程语言及许多第三方java框架( 如Spring,Structs等)构成. 可以把Java程序设 ...
- ES6 中的 Set
收录待用,修改转载已取得腾讯云授权 作者:kurtshen ES6 新增了几种集合类型,本文主要介绍Set以及其使用. 其基本描述为 Set对象是值的集合,你可以按照插入的顺序迭代它的元素. Set中 ...
- 怎样获取shell函数的返回值及shell命令的返回值?
1.获取shell函数调用的返回值: #!/bin/sh info() { cat jlb.sh } res=`info` echo "state: "$? echo " ...
- ambari 大数据安装利器
https://www.ibm.com/developerworks/cn/opensource/os-cn-bigdata-ambari/
- url: (6) Couldn’t resolve host ‘www.ttlsa.com’
http://www.ttlsa.com/linux/curl-6-couldnt-resolve-host/ 2. 解决问题 尝试解决方法: 修改dns # cat /etc/resolv.conf ...