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& ...
随机推荐
- rsync命令来实现将服务器端的文件备份到客户端
rsync命令来实现将服务器端的文件备份到客户端
- Powerful Regex
Online test Regex http://rubular.com An example src="http:\/\/localhost:3000\/preveiw-tool\/iph ...
- 导出Excel文件,npoi方式和通过microsoft.visual basic.dll
一:例子截图: 二:NPOI截图 三:EmployeeListWindow.cs代码 using System; using System.Collections.Generic; using Sys ...
- Unable to load native-hadoop library解决思路
最近试着搭建Hadoop,我使用的操作系统是Centos6.5,Hadoop版本是2.6.0. 在安装过程中总是出现:WARN util.NativeCodeLoader: Unable to loa ...
- tessilstrona
Untitled Document
- Cstyle的UEFI导读之SEC第一篇 Reset Vector
最近小看了一下SEC部分的code,现在来做个总结.所谓SEC就是CPU刚刚完成硬件初始化的是时候执行的和CPU体系架构息息相关的代码.主要是为后续CPU以及Chipset初始化代码所需的必备 ...
- Solr和ES对比
Solr与ES(ElasticSearch)对比 搜索引擎选择: Elasticsearch与Solr 搜索引擎选型调研文档 Elasticsearch简介* Elasticsearch是一个实时的分 ...
- VC6项目移植到VS2008的若干问题——好的代码,从我做起,从如今做起。
近期,有个项目开发,须要用到曾经项目的代码,只是曾经项目都是VC6下编译通过的,在VS2008下不一定编译通过,能编译通过也不一定能链接成功.以下总结一下我在一个VC6项目移植到VS2008中遇到的一 ...
- android中Canvas使用drawBitmap绘制图片
1.主要的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float ...
- STL——临时对象的产生与运用
所谓临时对象,就是一种无名对象.它的出现如果不在程序员的预期之下(例如任何pass by value操作都会引发copy操作,于是形成一个临时对象),往往造成效率上的负担.但有时候刻意制造一些临时对象 ...