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( 最小生成树 )
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...
随机推荐
- Git fetch & pull
转:https://blog.csdn.net/qq_36113598/article/details/78906882 1.简单概括 先用一张图来理一下git fetch和git pull的概念: ...
- 使用bootstrap的相关配置
<html> <head> <title>java微辅导</title> <meta charset="UTF-8"/> ...
- Python发邮件的小脚本
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText mailto_list = ['hitwh_Gy ...
- HTML5的manifest 本地离线缓存
下面直接放测试代码: index.html <!DOCTYPE html> <html manifest="m.manifest"> <head> ...
- 无线DOS攻击
1.无线连接状态 IEEE 802.11定义了一种客户端状态机制,用于跟踪工作站身份验证和关联状态.无线客户端和AP基于IEEE标准实现这种状态机制.成功关联的客户站停留在状态3,才能进行无线通信.处 ...
- js 布局转换问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- luogu P4568 [JLOI2011]飞行路线
传送门 看到免费次数\(k\)最多只有10,可以考虑构建\(k+1\)层的分层图,即每一层正常连边,上下两层对应点连边权为0的单向边,最后对所有层里面的\(di_t\)取\(\max\)救星了 #in ...
- luogu P3924 康娜的线段树
题面传送门 我们可以画图找规律 这里没图,要看图可以去看M_sea dalao的题解(逃 可以发现单个节点\(i\)对答案的贡献为该节点的点权\(*\frac{1}{2^{dep_i}}\)(\(de ...
- ActiveMQ学习笔记1
1.接口 JMS 公共 点对点域 发布/订阅域 ConnectionFactory QueueConnectionFactory TopicConnectionFactory Connection Q ...
- 【CTF WEB】ISCC 2016 web 2题记录
偶然看到的比赛,我等渣渣跟风做两题,剩下的题目工作太忙没有时间继续做. 第1题 sql注入: 题目知识 考察sql注入知识,题目地址:http://101.200.145.44/web1//ind ...