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个点的连 ...
随机推荐
- SQLAlchemy如何给列和表添加注释comment?
1.首先需要升级版本到1.2.x,我用的是1.2.14验证的,没有问题 2.看示例: class LoadResource(Base): """施压机资源."& ...
- 代码高亮 highlightjs 使用文档
使用方法 <link rel="stylesheet" href="../../assets/highlight/styles/default.css"& ...
- HTML:基本的标签
概述: <html></html>标准的语言格式,回环标签,有头和躯体部分,头里面一般显示标题title,躯体部分显示内容:背景色.文字.图片.超链接.表格.表单等. 可以直接 ...
- UVA 10790 (13.08.06)
How Many Points of Intersection? We have two rows. There are a dots on the toprow andb dots on the ...
- php 单进程SAPI生命周期
php的生命周期3.1 STARTUP 1.初始化引擎和核心组件. 2.解析php.ini. 3.初始化静态构建的模块(MINIT). 4.初始化共享模块(MINIT).3.2 ...
- 语音信号处理之(一)动态时间规整(DTW)
语音信号处理之(一)动态时间规整(DTW) zouxy09@qq.com 原文:http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门课,快考试了,所以也要 ...
- /etc/ssh/sshd_config 配置(ssh连接)
# Package generated configuration file# See the sshd_config(5) manpage for details # What ports, IPs ...
- [Firebase] 4. Firebase Object related Database
The idea: This post we are going to learn how to build a Firebase Forage with object related databas ...
- Ajax datatype:'JSON'的error问题Status1:200,JSON格式
转自:http://blog.sina.com.cn/s/blog_6e001be701017rux.html <script src="../js/jquery-1.8.0-vsdo ...
- Toast.makeText 方法出错 java.lang.RuntimeException
接手以前同事留下的代码,今天突然出现了一个bug: java.lang.RuntimeException: Can't create handler inside thread that has no ...