POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18472 | Accepted: 7318 |
Description
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.
题目描述
Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发。农场之间总共有M (1 <= M <= 10,000)条双向道路,所有道路的总长度不超过1,000,000,000。有些农场之间存在着多条道路,所有的农场之间都是连通的。
Bessie希望计算出该图中最小生成树中的最长边的长度。
思路
带并查集的最小生成树然后选择不加边而是将每一条加入的边和最小值比较
#include<cstdio>
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct note
{
int u;
int v;
int w;
} q[];
int f[];
int cmp(note a, note b)
{
return a.w<b.w;
}
int find(int v)
{
if (f[v] == v)return v;
else return find(f[v]);
}
int merge(int x, int y)
{
int t1, t2;
t1 = find(x);
t2 = find(y);
if (t1 != t2)
{
f[t2] = t1;
return ;
}
return ;
}
int main()
{
int i, k, n, m;
while (~scanf("%d%d", &n, &m))
{
for (i = ; i<m; i++)
scanf("%d%d%d", &q[i].u, &q[i].v, &q[i].w);
sort(q, q + m, cmp); //按照权值从小到大排序
for (i = ; i <= n; i++)
f[i] = i;
int count = ;
for (i = ; i<m; i++)
{
if (merge(q[i].u, q[i].v))//并查集看是否被连通
{
count++;
if (count == n - )
{
k = i; //最长的一条路的下标
break;
}
}
}
printf("%d\n", q[k].w);
}
}
POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)的更多相关文章
- POJ 2395 Out of Hay( 最小生成树 )
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...
- 瓶颈生成树与最小生成树 POJ 2395 Out of Hay
百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...
- POJ 2395 Out of Hay(最小生成树中的最大长度)
POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...
- Poj 2395 Out of Hay( 最小生成树 )
题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...
- POJ 2395 Out of Hay (prim)
题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...
- POJ 2395 Out of Hay
这个问题等价于求最小生成树中权值最大的边. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- poj 2395 Out of Hay(最小生成树,水)
Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- POJ 2395 Out of Hay(MST)
[题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...
随机推荐
- kafka生产者和消费者流程
前言 根据源码分析kafka java客户端的生产者和消费者的流程. 基于zookeeper的旧消费者 kafka消费者从消费数据到关闭经历的流程. 由于3个核心线程 基于zookeeper的连接器监 ...
- 子网掩码与ip有实际关系吗?
子网掩码是作为ip地址的标识,还是本身就是ip地址的一部分?例如10.10.10.1/24和10.10.10.1/25是同一个ip地址吗? 作者:知乎用户链接:https://www.zhihu.co ...
- PowerDesigner16工具学习笔记-建立BPM
根据不同用途,BPM分为分析性(Analysis).执行型(Executable)和协作型(Collaborative) BPM的类型 业务流程语言 描述 分析型 Analysis 提供流程层次 ...
- 本地Run Page时报检测到意外的 URL 参数,它将被忽略。
经查,是因为我RUN的时候 Target URL -- http://MY-PC:8988/OA_HTML/runregion.jsp 将MY-PC更改为本地IP即可. 设置方法 Tools - Em ...
- IOS UIApplication和AppDelegate 关系
UIApplication.AppDelegate.委托等的关系? 什么是委托?为什么要有委托?委托在Iphone中的实现机制是怎样的? 一般来说,我们创建了一个Iphone项目,默认会有这个mai ...
- Kotlin Reference (十) Interfaces
most from reference 接口 Kotlin中的接口非常类似于Java8,它们可以包含抽象方法的声明以及方法实现.与抽象类不同的是接口不能存储状态.它们可以具有属性,但这些需要是抽象的或 ...
- UE4 游戏中csv配置文件使用
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/76549463 作者:ca ...
- 在Windows中配置Rsync同步
Rsync是一款不错的文件免费同步软件,可以镜像保存整个目录树和文件系统,同时保持原来文件的权限.时间.软硬链接.第一次同步时 rsync 会复制全部内容,下次只传输修改过的文件部分.传输数据过程中可 ...
- yaf路由配置规则
使用框架的默认路由来访问的时候,会遇到一些困扰,这部分无法查看源代码,只能通过猜测来分析. 如果项目有多个模块,显然使用yaf的默认的静态路由是无法满足需求的. yaf默认的配置是着这样的: appl ...
- 【C++基础】sort函数
sort函数的时间复杂度为O(n*logn),排序方法类似于快排. # 头文件 #include<algorithm> using namespace std; # 参数 第一个参数:要排 ...