POJ 1861 ——Network——————【最小瓶颈生成树】
| Time Limit: 1000MS | Memory Limit: 30000K | |||
| Total Submissions: 15268 | Accepted: 5987 | Special Judge | ||
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
4
1 2
1 3
2 3
3 4
Source
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1010;
const int maxe = 15010;
struct Edge{
int from,to,dist,idx;
Edge(){}
Edge(int _from,int _to,int _dist,int _idx):from(_from),to(_to),dist(_dist),idx(_idx){}
}edges[maxe];
struct Set{
int pa,rela;
}sets[maxn];
int ans[maxn];
bool cmp(Edge a,Edge b){
return a.dist < b.dist;
}
void init(int n){
for(int i = 0; i <= n; i++){
sets[i].pa = i;
}
}
int Find(int x){
if(x == sets[x].pa){
return x;
}
int tmp = sets[x].pa;
sets[x].pa = Find(tmp);
return sets[x].pa;
}
int main(){
int n, m;
while(scanf("%d%d",&n,&m)!=EOF){
init(n);
int a,b,c;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
edges[i] = Edge(a,b,c,i);
}
sort(edges,edges+m,cmp);
int cnt = 0;
for(int i = 0; i < m; i++){
Edge & e = edges[i];
int rootx, rooty;
rootx = Find(e.from);
rooty = Find(e.to);
if(rootx == rooty){
continue;
}
sets[rooty].pa = rootx;
ans[cnt++] = i;
}
printf("%d\n",edges[ans[cnt-1]].dist);
printf("%d\n",cnt);
for(int i = 0; i < cnt; i++){
printf("%d %d\n",edges[ans[i]].from,edges[ans[i]].to);
}
}
return 0;
}
POJ 1861 ——Network——————【最小瓶颈生成树】的更多相关文章
- 【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)
[题意] n个点m条边的图 q次询问 找到一条从s到t的一条边 使所有边的最大危险系数最小 InputThere will be at most 5 cases in the input file.T ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- 【最小瓶颈生成树】【最小生成树】【kruscal】bzoj1083 [SCOI2005]繁忙的都市
本意是求最小瓶颈生成树,但是我们可以证明:最小生成树也是最小瓶颈生成树(其实我不会).数据范围很小,暴力kruscal即可. #include<cstdio> #include<al ...
- 【bzoj2429】[HAOI2006]聪明的猴子(图论--最小瓶颈生成树 模版题)
题意:有M只猴子,他们的最大跳跃距离为Ai.树林中有N棵树露出了水面,给出了它们的坐标.问有多少只猴子能在这个地区露出水面的所有树冠上觅食. 解法:由于要尽量多的猴子能到达所有树冠,便用Kruskal ...
- POJ 1861 Network (MST)
题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. //#include <bits/stdc++.h> #includ ...
- BZOJ 3732 Network 最小瓶颈路
题目大意:给出一个无向边,非常多询问,问x,y两地之间的最长路最短是多少. 思路:乍一看好像是二分啊. 的确这个题二分能够做.可是时间会慢非常多,有的题直接就T掉(NOIP2013货车运输). 事实上 ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
随机推荐
- EIP权限工作流升级说明-2019/3/5
首页增加待办事项直接处理按钮 2,新增处理历史记录
- JavaAppArguments示例
本实验要求编写一个程序,此程序从命令行接收多个数字,求和之后输出结果.一大难点是命令行参数都是字符串,必须先将其转化为数字,才能相加. 中心想法就是将求和数字转换为整型并依次相加. 程序流程图: pu ...
- 692. Top K Frequent Words
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- loj#6229 这是一道简单的数学题
\(\color{#0066ff}{ 题目描述 }\) 这是一道非常简单的数学题. 最近 LzyRapxLzyRapx 正在看 mathematics for computer science 这本书 ...
- 10.18 NOIP2018提高组模拟题(二)
大水题 1.咒语 (curse.pas/c/cpp) [题目描述] 亮亮梦到自己来到了魔法城堡,但一扇巨大的石门阻拦了他通向城堡内的路.正当他沮丧之际,突然发现门上有一处机关,机关上有一张很长的纸条. ...
- Vue中添加过渡效果
最近在学习Vue这个框架,发现新的版本中,官网的文档里面说的过渡效果,如果直接粘贴官方的例子中的代码,发现并没有过渡的效果,经过反复测试之后终于知道怎么搞了,把测试的过程总结一下,以便以后回顾. 贴上 ...
- git 打tag
查看已有tag git tag 创建新的tag git tag <version or tagname> -m <tag description> 例如创建一个版本1.0.0的 ...
- [POI2009]KAM-Pebbles BZOJ1115 [ 待填坑 ] 博弈
有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏.问先手是否必胜. 感谢 ...
- Linux系统之ssh命令
ssh命令用于远程登录上Linux主机. 常用格式:ssh [-l login_name] [-p port] [user@]hostname更详细的可以用ssh -h查看. 不指定用户: ssh 1 ...
- php fsockopen使用
函数说明:fsockopen — 打开一个网络连接或者一个Unix套接字连接 语法: resource fsockopen ( string $hostname [, int $port = -1 [ ...