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 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...
随机推荐
- 一个轻量级分布式 RPC 框架 — NettyRpc
原文出处: 阿凡卢 1.背景 最近在搜索Netty和Zookeeper方面的文章时,看到了这篇文章<轻量级分布式 RPC 框架>,作者用Zookeeper.Netty和Spring写了一个 ...
- 解决QML Window 增加radius效果
做开发时,突然遇到 一个需要模态展示的对话框,做出来后,发现还要radius属性,增加时发现,Window控件不支持这个属性.如果是以前,原本就打算放弃了,但想一下,这种应该是支持的,既然接口上没有, ...
- python yaml文件读写
import yaml yaml_dict={"} with open("a.yaml", "w") as f: yaml.safe_dump(yam ...
- TTL的具体含义
TTL(Time To Live)生存时间值,在IP数据包从源到目的的整个转发路径上,每经过一个路由器,路由器都会修改这个TTL字段值,具体的做法是把该TTL的值减1,然后再将IP包转发出去.如果在I ...
- 将Excel的数据更新至SqlServer
update P_Dic_Region set F_Long=a.F_Long,F_Lat=a.F_Lat from( select * from OPENDATASOURCE( 'Microsoft ...
- 2-14-1 MySQL基础语句,查询语句
一. SQL概述 结构化查询语言(Structured Query Language)简称SQL 1. 它是一种特殊目的的编程语言 2. 它还是一种数据库查询和程序设计语言 (用于存取数据以及查询.更 ...
- 无法安装64位版本的office因为在您的pc
无法安装64位版本的office因为在您的pcWindows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Installer\Products\00 ...
- Codeforces Round #279 (Div. 2) 题解集合
终于有场正常时间的比赛了...毛子换冬令时还正是好啊233 做了ABCD,E WA了3次最后没搞定,F不会= = 那就来说说做的题目吧= = A. Team Olympiad 水题嘛= = 就是个贪心 ...
- xml生成javabean(zhuan)
package com.dom4j; import java.io.File;import java.io.FileWriter;import java.io.IOException;import j ...
- 中行用户购买KIS2014 68元/3年,时间:2013.10.18-2013.11.18
活动地址:http://boc.kaba365.com/4989800.asp