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& ...
随机推荐
- 关于Mysql当中"Got error 134 from storage engine"的解决办法
今天在开发程序的时候,有一个表, 当调用这个类别时总是调用不出来,很是恼火.后台打印sql语句为: SELECT * FROM `xx_article` WHERE `cid1` =6 LIMIT 0 ...
- MongoDB权限提升漏洞(CVE-2013-4650)
漏洞版本: MongoDB 2.4.0-2.4.4 MongoDB 2.5.0 漏洞描述: CVE ID: CVE-2013-4650 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前N ...
- 安装配置MongoDB
1.下载mongodb https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.8.tgz 2.解压 tar zxf mongodb-lin ...
- delphi NativeXml的中文支持 乱码
一般XML的编码格式设置成UTF8比较通用,下面演示使用UTF8编码方式存储和处理包含中文的XML字符串(文件).1.设置启用内置的widestring支持 NativeXml内部使用ANSI str ...
- zoj 3672 Gao The Sequence
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4915题意:a[k]-一个任意的数,这个数要等于a[1]~a[k]每个数减去任意 ...
- Rotate List —— LeetCode
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- cf703C Chris and Road
C. Chris and Road time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- MD5加密详解
MD5加密详解 引言: 我在百度百科上查找到了关于MD5的介绍,我从中摘要一些重要信息: Message Digest Algorithm MD5(中文名为信息摘要算法第五版)为计算机安全领域广泛使用 ...
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
jedis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分布式 ...
- Hibernate一 入门
一 简介1.什么是ORMObject/Relation Mapping,即对象/关系映射.可以将其理解为一种规范,具体的ORM框架可以作为应用程序和数据库的桥梁.面向对象程序设计语言与关系数据库发展不 ...