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 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...
随机推荐
- POJ - 1463 Strategic game (树状动态规划)
这题做的心塞... 整个思路非常清晰,d[i][0]表示第i个结点不设置监察的情况下至少需要的数量:d[i][1]表示第i个结点设置检查的情况下的最小需要的数量. 状态转移方程见代码. 但是万万没想到 ...
- 如何解决Nginx php 50x 错误
SEO反馈百度爬虫经常504,一般情况下是由nginx默认的fastcgi进程响应慢引起的,但也有其他情况,这里我总结了一些解决办法供大家参考. 方法/步骤 一般50x状态码问题分析: Nginx ...
- c++ 判断容器是否为空
#include <iostream> #include <vector> #include <string> using namespace std; int m ...
- myeclipse:web项目不能显示Web App Libraries
项目根路径下.classpath文件,加上 <classpathentry kind="con" path="org.eclipse.jst.j2ee.intern ...
- 如何写一篇好的技术博客or技术文档(转链接)
如何写一篇好的技术文档http://yunli.blog.51cto.com/831344/168352 程序员怎样才能写出一篇好的博客或者技术文章?http://www.zhihu.com/ques ...
- flask学习(十三):过滤器
1. 介绍和语法 介绍:过滤器可以处理变量,把原始的变量经过处理后再展示出来,作用的对象是变量
- UVA-11082 Matrix Decompressing (网络流建模)
题目大意:给出一个由1到20组成的整数矩阵的每一行和每一列的和,构造这个矩阵.输出任意一个构造方案. 题目分析:将每一行视作一个点x,将每一列视作一个点y.对于矩阵中的每一个格子,都对应一个二元关系& ...
- 适配器 STL
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- openfalcon源码分析之transfer
本节内容 transfer功能 transfer接收数据来源 transfer数据去向 transfer的一致性hash transfer的一致性hash key的计算 transfer源码分析 2. ...
- 发现TypeScript中同名interface接口会自动合并的特性
今天在学习怎么用TypeScript给jQuery写扩展插件时发现一个很有趣的事情