POJ 2395 Out of Hay(最小生成树中的最大长度)
本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数,
判断ans==n-1时,即找到了最大边。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
int par[];
int ran[];
struct edge{
int from,to,cost;
};
edge es[];
bool cmp(const edge& x,const edge& y){
return x.cost<y.cost;
}
void init(int n){
for(int i=;i<n;i++){
par[i]=i;
ran[i]=;
}
}
int find(int x){
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
void unite(int x,int y){
x=find(x);
y=find(y);
if(x==y) return ;
if(ran[x]<ran[y]) par[x]=y;
else{
par[y]=x;
if(ran[x]==ran[y])
ran[x]++;
}
}
bool same(int x,int y){
return find(x)==find(y);
}
int main()
{
int n,m,k,res;
scanf("%d%d",&n,&m);
int a,b,c;
k=;
while(m--){
scanf("%d%d%d",&a,&b,&c);
a--;b--;
es[k].from=a;
es[k].to=b;
es[k++].cost=c;
es[k].from=b;
es[k].to=a;
es[k++].cost=c;
}
sort(es,es+k,cmp);
init(n);//注意并查集的初始化
int ans=;
for(int i=;i<k;i++){
edge e=es[i];
if(!same(e.to,e.from)){
unite(e.to,e.from);
ans++;//单独标记已用过的边数
if(ans==n-){
res=e.cost;
break;
}
}
}
printf("%d\n",res);
return ;
}
POJ 2395 Out of Hay(最小生成树中的最大长度)的更多相关文章
- Poj 2395 Out of Hay( 最小生成树 )
题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...
- 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
百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶 ...
- POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18472 Accepted: 7318 Descr ...
- POJ 2395 Out of Hay( 最小生成树 )
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...
- 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 (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& ...
随机推荐
- web.py simpletodo 例子
一个很好的例子: 许多新手,特别是从 ASP/PHP/JSP 转过来的同学,经常问下面这几个问题: 所有东西都放在一个 code.py 中呀?我有好多东西该如何部署我的代码? 是不是 /index 对 ...
- Unity NGUI制作scroll view
unity版本:4.5 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/monzart7an/article/details/23878505,作者:CSDN 冬菊子 ...
- Qt入门(14)——父窗口部件和子窗口部件
这个例子演示了如何创建一个父窗口部件和子窗口部件.我们下面使用一个单一的父窗口部件和一个独立的子窗口部件编写界面. #include <qvbox.h>我们添加了一个头文件qvbox ...
- 动态树(Link Cut Tree) :SPOJ 375 Query on a tree
QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...
- HDOJ(HDU) 2123 An easy problem(简单题...)
Problem Description In this problem you need to make a multiply table of N * N ,just like the sample ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- Appium移动自动化测试(三)--安装Android模拟器(转)
Appium移动自动化测试(三)--安装Android模拟器 2015-06-08 10:33 by 虫师, 30828 阅读, 9 评论, 收藏, 编辑 当Android SDK安装完成之后,并不意 ...
- javascrip实现无缝滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 跨平台通信中间件thrift学习【Java版本】(转)
转自:http://neoremind.com/2012/03/%E8%B7%A8%E5%B9%B3%E5%8F%B0%E9%80%9A%E4%BF%A1%E4%B8%AD%E9%97%B4%E4%B ...
- 深入了解JavaScript中的for循环
在ECMAScript5中,有三种for循环,分别是: 简单for循环 for-in forEach 在ES6中,新增了一种循环 for-of 简单for循环 const arr = [1, 2, 3 ...