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( 最小生成树 )
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...
随机推荐
- 【转载】Etcd+Confd实现Nginx配置文件自动管理
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lizhenliang.blog.51cto.com/7876557/191015 ...
- 转--python -- 收发邮件
官方 import smtplib from email.mime.text import MIMEText from email.header import Header # 发送邮箱服务器 smt ...
- 7.SpringBoot 之 Web
添加资源处理 package org.springframework.boot.autoconfigure.web.servlet. public class WebMvcAutoConfigurat ...
- android SQLiteOpenHelper 使用
1.实体 package mydemo.mycom.demo2.entity; public class UserInfo { private int id; private String usern ...
- 字体QFont
import sys from PyQt5.QtWidgets import QApplication, QWidget,QLabel from PyQt5.QtGui import QFont fr ...
- luogu P4160 [SCOI2009]生日快乐
传送门 考虑因为每个人的蛋糕体积要相等,如果切了一刀,那么要使得分当前蛋糕的人根据分成的两部分蛋糕的体积分成两部分人,所以假设当前有n人,切的这一刀要是在x或y的\(\frac{k}{n}(k\in ...
- luogu P1593 因子和
不要吐槽博主总做这些数论氵题 首先我们看到这种因数问题,果断质因数分解 所以当前数\(a=p_1^{k_1}*p_2^{k_2}...*p_m^{k_m}\) 可得\(a^b=p_1^{k_1*b}* ...
- ZOC7 for Mac连接CentOS7无法输入中文问题
确定是ZOC7的问题 改为 iTerm2 加 ZSH 能够输入中文了 自己配置profile 慢慢所有的终端都用iTerm2 渐渐放弃ZOC7
- python - 系统交互操作(subprocess)
本文摘于云游道士 链接:https://www.cnblogs.com/yyds/p/7288916.html 个人简化,便于查询. 命令行指令的执行通常有两个比较关注的结果: 命令执行的状态码--表 ...
- 详解Jquery选择器
1.常见的选择器 id,类,标签选择器. $("#a1") $(".myclass") $("div") 2.组合选择器 $("# ...