Out of Hay
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18472   Accepted: 7318

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.

题目描述

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)的更多相关文章

  1. POJ 2395 Out of Hay( 最小生成树 )

    链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...

  2. 瓶颈生成树与最小生成树 POJ 2395 Out of Hay

    百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...

  3. POJ 2395 Out of Hay(最小生成树中的最大长度)

    POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...

  4. Poj 2395 Out of Hay( 最小生成树 )

    题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...

  5. POJ 2395 Out of Hay (prim)

    题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...

  6. POJ 2395 Out of Hay

    这个问题等价于求最小生成树中权值最大的边. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  7. poj 2395 Out of Hay(最小生成树,水)

    Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...

  8. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  9. POJ 2395 Out of Hay(MST)

    [题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...

随机推荐

  1. Java,vue.js,jsp for循环的写法

    vue.js <li v-for="student in studentList">{{student.name}}</li> jsp el表达式 < ...

  2. 递推-练习2--noi3525:上台阶

    递推-练习2--noi3525:上台阶 一.心得 二.题目 3525:上台阶 总时间限制:  1000ms 内存限制:  65536kB 描述 楼梯有n(100 > n > 0)阶台阶,上 ...

  3. UVA-1613 K-Graph Oddity (着色问题)

    题目大意:一张n个顶点.m条边的无向连通图,用k种颜色着色(相邻顶点颜色不能相同),其中k为不小于点的最大度数的最小奇数. 题目分析:水题一道.建张图深搜一下就行了. # include<ios ...

  4. 转 CentOS 7 minimal 版本安装后网络配置

    转自[http://www.07net01.com/2016/01/1140061.html] 1.首先使用root登录服务器,输入 nmcli d 我们发现网卡是处于禁用状态. 2.打开网络管理器界 ...

  5. IOS-快速集成检查更新

    一直以为Appstore有了检查版本是否更新的机制,我们在APP上做这个更新功能会被拒,但是也有看到一些APP也是做了这个更新功能的.因为在网上没有找到完全正确的方法能获取到iTunes里的数据的,于 ...

  6. gradle ssh 插件

    org.hidetake.ssh Gradle SSH Plugin is a Gradle plugin which provides remote command execution and fi ...

  7. 056——VUE中vue-router之路由参数的验证处理保存路由安全

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. asp.net中的时间日期选择控件

    asp.net中的时间日期选择控件 Posted on 2008-07-17 17:37 飛雪飄寒 阅读(22922) 评论(6) 编辑 收藏     在系统中经常需要进行时间日期选择(比如查询时间范 ...

  9. cell 重用

    1. 当单元格因滚屏而脱落表格可见区时,表格可以将其缓存到重用队列中. 用法:我们可标记单元格以备重用,然后根据需要从该队列中提取. 在分配新单元格时,必须检查重用单元格是否可用.如果表格对deque ...

  10. SSH整合报错:Unable to instantiate Action, testAction, defined for 'test' in namespace '/'testAction

    报错如下: Struts Problem Report Struts has detected an unhandled exception: Messages: testAction Unable ...