POJ 2395 Out of Hay (Kruskal)
题意:从待选的路里面选出若干将所有点连通,求选出的边里最长边的最小值。
算法:要使得树的最长边最小,那么每次确定的边都应是待选边里最小的,即最小生成树。对应Kruskal算法。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; int N, M; // 农场数,道路数
int par[2005];
void init() {
for (int i = 1; i <= N; ++i) par[i] = i;
}
int find(int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) par[x] = y;
}
bool same(int x, int y) {
return find(x) == find(y);
}
struct edge {
int u, v, cost;
edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
bool operator<(const edge &b) const {
return cost < b.cost;
}
};
vector<edge> es;
void solve() {
sort(es.begin(), es.end());
init();
for (vector<edge>::iterator i = es.begin(); i != es.end(); ++i) {
if (!same(i->u, i->v)) {
unite(i->u, i->v);
N--;
}
if (N == 1) { // 第N-1条边
cout << i->cost << endl;
break;
}
}
}
int main()
{
cin >> N >> M;
int u, v, cost;
for (int i = 0; i < M; ++i) {
cin >> u >> v >> cost;
es.push_back(edge(u, v, cost));
}
solve();
return 0;
}
POJ 2395 Out of Hay (Kruskal)的更多相关文章
- 瓶颈生成树与最小生成树 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(求最小生成树的最长边+kruskal)
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18472 Accepted: 7318 Descr ...
- 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 2395 Out of Hay(最小生成树,水)
Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll ...
- 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( 最小生成树 )
题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...
- POJ 2395 Out of Hay( 最小生成树 )
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...
随机推荐
- HDU - 5413 CRB and Roads
CRB and Roads Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- nginx配置url中带问号的rewrite跳转
今天收到一个需求,要将一个带查询参数的url跳转到另外一个静态url,安装常规的rewrite规则,如: rewrite ^/a.html?id=67$ http://zt.epython.cn/20 ...
- JS 互相调用iframe页面中js方法、VUE里 iframe 互调方法
1,父 html 调用子 iframe 内方法: document.getElementById("iframe").contentWindow.func(data1,data2. ...
- Android studio 自动导入(全部)包 import
http://blog.csdn.net/buaaroid/article/details/44979629 1 Android studio 只有import单个包的快捷键:Alt+Enter.没有 ...
- 二、编译第一步 make xxx_defconfig
2.1 顶层make defconfig规则 make xxx_defconfig 的执行主要分成三个部分: 执行 make -f ./scripts/Makefile.build obj=scrip ...
- C++ error LNK2001
1.没有函数的实现部分 2.函数的头文件和实现部分不一致 3.缺库LIB.DLL 4.库H文件函数和库体不一致
- C# 简单线程实例
1.简单线程实例 以及委托(同步委托.异步委托) using System; using System.Collections.Generic; using System.Linq; using Sy ...
- 阿里云3台机器搭建Hadoop HA服务
1 Mac电脑配置 阿里云配置机器 选择配置 按量付费 选择三台机器 2核8G
- 零基础http代理http完美代理访问
如果翻过墙,或者做过渗透啥的,肯定对代理不陌生,说白了,代理服务器就是一个中转站,你对目标网址的请求都会进过代理服务器去请求,类似于一个被你操控的傀儡,别人能知道的也只能是这个代理,从而提升安全性和访 ...
- Linux CentOS 服务器搭建与初始化配置图文详解
这几天对服务器兴趣贼为浓厚,在虚拟机上装了一个CentOS7玩了玩,遇到过很多问题,比如网卡驱动设置,不能ping 等等问题,然后掏钱买个ECS搭服务器玩玩,下面就开始谢谢我的心路历程吧. 首先 买服 ...