POJ1861 Network (Kruskal算法 +并查集)
Network
Description
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.
Input
Output
Sample Input
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
Sample Output
1
3
1 2
1 3
3 4
题解:原题目数据输出有问题,该题就是典型的Krusal算法,这里使用并查集判断有没有环形成。
#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=20000;
int pre[maxn],height[maxn];
void init_set(int n){
for (int i = 1; i <= n; i++){
pre[i]=i;
}
memset(height,0,sizeof(height));
} int find_set(int x){
return x==pre[x]?x:pre[x]=find_set(pre[x]);
}
void union_set(int x,int y){
x= find_set(x);
y= find_set(y);
if(x==y)return;
if(height[x]==height[y]){
height[x]=height[x]+1;
pre[y]=x;
}else{
if(height[x]<height[y]) pre[x]=y;
else{
pre[y]=x;
}
}
} struct edge{
int u,v,w;
bool operator <(const edge& a)const{
return w<a.w;
}
};
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
init_set(n);
edge* edges = new edge[m+1];
bool *record=new bool[m+1];
// memset(record,false,sizeof(record));
for (int i = 1; i <=m; i++){
record[i]=false;
scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);
}
sort(edges+1,edges+m+1);
int maxcable=edges[0].w,sums=0,num=0;
int u,v;
for (int i = 1; i <=m; i++){
u=edges[i].u;
v=edges[i].v;
if(find_set(u)!=find_set(v)){
union_set(u,v);
num++;
record[i]=true;
}
if(num>=n-1){
maxcable=edges[i].w;
break;
}
}
cout<<maxcable<<endl;
cout<<n-1<<endl;
for (int i = 1; i<=m; i++){
if(record[i]){
cout<<edges[i].u<<" "<<edges[i].v<<endl;
}
}
}
}
POJ1861 Network (Kruskal算法 +并查集)的更多相关文章
- 最小生成树(Minimum Spanning Tree)——Prim算法与Kruskal算法+并查集
最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻 ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 图论 Kruskal算法 并查集
#include<iostream> #include<cstring> #include<string> #include<cstdio> #incl ...
- poj1251 Jungle Roads Kruskal算法+并查集
时限: 1000MS 内存限制: 10000K 提交总数: 37001 接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超 ...
- 货车运输-洛谷-1967-LCA+最大生成树(kruskal(并查集))
传送门 一道:LCA+最大生成树 个人认为把这两个的板子写好(并熟练掌握了之后)就没什么难的 (但我还是de了好久bug)qwq 最大生成树:其实就是最小生成树的变形 我用的是kruskal (个人觉 ...
- BZOJ 2342: [Shoi2011]双倍回文 马拉车算法/并查集
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1123 Solved: 408 题目连接 http://w ...
- 近期公共祖先(LCA)——离线Tarjan算法+并查集优化
一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
随机推荐
- Logback 快速入门 / 使用详解
官方文档: http://logback.qos.ch/manual/index.html 一.简介 Java 开源日志框架,以继承改善 log4j 为目的而生,是 log4j 创始人 Ceki Gü ...
- 解决ftp登录问题:500 OOPS: cannot change directory:/home/xxx 500 OOPS: child died
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- Setup a Simple HTTP Proxy Server
The host 10.21.3.69 has no H3C client, so it can't access the internet. With Tinyproxy, we can setuu ...
- Java程序设计(2021春)——第四章接口与多态课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第四章接口与多态课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第四章接口与多态课后题(选择题+编程题)答案与详解 第四章选择题 4.0 ...
- 012 基于FPGA的网口通信实例设计【转载】
一.网口通信设计分类 通过上面其他章节的介绍,网口千兆通信,可以使用TCP或者UDP协议,可以外挂PHY片或者不挂PHY片,总结下来就有下面几种方式完成通信: 图8‑17基于FPGA的网口通信实例设计 ...
- Liunx搭建Mysql服务器
1:安装从网上下载文件的wget命令[root@master ~]# yum -y install wget 2:下载mysql的repo源[root@master ~]# wget http://r ...
- SQL 练习7
查询所有同学的学生编号.学生姓名.选课总数.所有课程的总成绩(没成绩的显示为 null ) SELECT Student.SId,sname,t.选课总数,t.总成绩 from Student LEF ...
- noip13
T1 一开始直接丢了个暴力走人50pts,然后开始打表找规律,啥也没找着,最后二十分钟突然看出来了什么,把 \(f_{n,m}\)式子列了一下,发现常数项没啥规律,最后五分钟,突然闪过一丝灵感,但是是 ...
- 数据结构与算法-排序(十)桶排序(Bucket Sort)
摘要 桶排序和基数排序类似,相当于基数排序的另外一种逻辑.它是将取值范围当做创建桶的数量,桶的长度就是序列的大小.通过处理比较元素的数值,把元素放在桶的特定位置,然后遍历桶,就可以得到有序的序列. 逻 ...
- kafka查看Topic列表及消费状态等常用命令
环境 本文中的操作均基于kafka_1.3.3.0,且所有命令经过实际验证. 常用工具 新建Topic ./kafka-topics --zookeeper 166.188.xx.xx --creat ...