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& ...
随机推荐
- struts2 Convention插件零配置,使用注解开发
从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...
- COJ 2004 序列
传送门:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=978 试题描述: WZJ的数字游戏又开始了.他写了N个自然数Ai到黑 ...
- Poetize4 创世纪
3037: 创世纪 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 123 Solved: 66[Submit][Status] Description ...
- Linux企业级开发技术(7)——libevent企业级开发之锁和线程
编写多线程程序的时候,在多个线程中同时访问同样的数据并不总是安全的. libevent的结构体在多线程下通常有三种工作方式: 1.某些结构体内在地是单线程的:同时在多个线程中使用它们总是不安全的. 2 ...
- (转载)HTML标签<br><br/>的区别在哪里?
(转载)http://zhidao.baidu.com/question/259205863.html HTML标签<br><br/>的区别在哪里? 如果一样为什么还要分2个标 ...
- UVaLive5031 Graph and Queries(时光倒流+名次树)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20332 [思路] 时光倒流+名次树(rank tree). 所谓“ ...
- [Locked] Longest Substring with At Most Two Distinct Characters
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
- lightoj 1004 dp:数字三角形
题目链接:http://lightoj.com/volume_showproblem.php?problem=1004 #include <cstdio> #include <cst ...
- Azkaban2配置过程
Azkaban2配置过程 azkaban2所需环境:jdk1.6.ant.jetty.hadoop.ssl证书 通过http://azkaban.github.io/azkaban2/download ...
- raw_input() 与 input()的区别
raw_input和input两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同.下面举两个例子,来说明两者 raw_input和input两个均是 pyth ...