POJ 1861 Network (Kruskal求MST模板题)
| Time Limit: 1000MS | Memory Limit: 30000K | |||
| Total Submissions: 14103 | Accepted: 5528 | Special Judge | ||
Description
worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).
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
from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There
will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.
Output
of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.
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
题目链接:poj.org/problem?
id=1861
题目大意:n个点,m条线,每条线有个权值,如今要求最长的路最短且让各个点都连通,求最短的最长路,边个数和相应边
题目分析:例子有问题,应该是
1
4
1 2
1 3
3 4
裸的Kruskal注意这里要求最长路最短,而Kruskal正好是对权值从小到大排序后的贪心算法
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 15005;
int fa[MAX];
int n, m, ma, num;
int re1[MAX], re2[MAX]; struct Edge
{
int u, v, w;
}e[MAX]; bool cmp(Edge a, Edge b)
{
return a.w < b.w;
} void UF_set()
{
for(int i = 0; i < MAX; i++)
fa[i] = i;
} int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} void Union(int a, int b)
{
int r1 = Find(a);
int r2 = Find(b);
if(r1 != r2)
fa[r2] = r1;
} void Kruskal()
{
UF_set();
for(int i = 0; i < m; i++)
{
int u = e[i].u;
int v = e[i].v;
if(Find(u) != Find(v))
{
re1[num] = u;
re2[num] = v;
Union(u, v);
ma = max(ma, e[i].w);
num ++;
}
if(num >= n - 1)
break;
}
} int main()
{
ma = 0;
num = 0;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++)
scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
sort(e, e + m, cmp);
Kruskal();
printf("%d\n%d\n", ma, num);
for(int i = 0; i < num; i++)
printf("%d %d\n", re1[i], re2[i]);
}
POJ 1861 Network (Kruskal求MST模板题)的更多相关文章
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- POJ 1258 Agri-Net 【Prime】模板题
题目链接>>> 题目大意: 给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <c ...
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- POJ 1860 Currency Exchange【bellman-Ford模板题】
传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- [poj1144]Network(求割点模板)
解题关键:割点模板题. #include<cstdio> #include<cstring> #include<vector> #include<stack& ...
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
随机推荐
- PLC学习资料
常用字母 X 输入点(I)可连接外部输入信号 如感应器或限位/按钮等M 内部辅助继电器S 内部步进 不作步进使用时,可用作内部辅助继电器T 时间继电器 内部使用C计数器 内部使用Y输出点(O)输出给外 ...
- opencv函数之cv.InRange函数
2018-03-0421:22:46 (1)cv.InRange函数 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像const CvArr ...
- vuex理解之modules小记
好记性不如烂笔头 demo预览 源代码 前情提要 关于vuex,其实很久以前就研究使用过,还研究过 flux,redux之类的体系,当时感觉对于 state,action,dispatch,views ...
- Java 基础入门随笔(9) JavaSE版——文档注释
上节中写了一些static变量以及静态的方法的定义使用以及与非静态的差别,这节补充下: 如果在一个类中所有方法都为静态的,且无成员变量,这时候需要对对应的类进行限制该类无法创建对象,具体操作如下: p ...
- dutacm.club_1087_Common Substrings_(KMP)_(结合此题通俗理解kmp的next数组)
1087: Common Substrings Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/ ...
- CAD得到布局名
js代码如下: var database = mxOcx.GetDatabase(); var sRet = null; //返回数据库中的布局字典 var spLayoutDictionary = ...
- 什么是 C 和 C ++ 标准库?
简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...
- enote笔记语言(2)(ver0.4)
why not(whyn't) 为什么不(与“why”相反对应,是它的反面) how对策 how设计 key-memo: ...
- <SpringMvc>入门七 拦截器
什么是拦截器 1.SpringMVC框架中的拦截器用于 对处理器 进行预处理和后处理的技术. 2.可以定义拦截器链,按照顺序执行. 3.拦截器和过滤器功能类似,区别在 拦截器 过滤器 过滤器是Serv ...
- linux(Ubuntu16)下切换python2和python3(转)
采用update-alternatives 切换版本 1.打开终端:Ctrl+Alt+T 2.查看update-alternatives的帮助信息:update-alternatives --help ...