ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接: problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络
Network
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables.
Since each 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
The first line of the input file contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000).
All hubs are numbered 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
10^6. 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.
Process to the end of file.
Output
Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then
output P pairs 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
分析:求最小生成树中的最长边的值。然后输出所选择的边。Kruskal算法。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std; #define maxn 1010
#define maxm 15002
int n, m, maxlen, cnt;
int parent[maxn], ans[maxn];
struct edge
{
int u, v, w;
}EG[maxm]; bool cmp(edge a, edge b)
{
return a.w < b.w;
}
int Find(int x)
{
if(parent[x] == -1) return x;
return Find(parent[x]);
}
void Kruskal()
{
memset(parent, -1, sizeof(parent));
sort(EG, EG+m, cmp);
for(int i = 0; i < m; i++)
{
int t1 = Find(EG[i].u), t2 = Find(EG[i].v);
if(t1 != t2)
{
if(maxlen < EG[i].w)
maxlen = EG[i].w;
ans[cnt++] = i;
parent[t1] = t2;
}
}
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
for(int i = 0; i < m; i++)
scanf("%d%d%d", &EG[i].u, &EG[i].v, &EG[i].w);
maxlen = cnt = 0;
Kruskal();
printf("%d\n", maxlen);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
printf("%d %d\n", EG[ans[i]].u, EG[ans[i]].v);
}
return 0;
}
ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法的更多相关文章
- 【数据结构】 最小生成树(四)——利用kruskal算法搞定例题×3+变形+一道大水题
在这一专辑(最小生成树)中的上一期讲到了prim算法,但是prim算法比较难懂,为了避免看不懂,就先用kruskal算法写题吧,下面将会将三道例题,加一道变形,以及一道大水题,水到不用高级数据结构,建 ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
- POJ 1861 Network
题意:有n个点,部分点之间可以连接无向边,每条可以连接的边都有一个权值.求一种连接方法将这些点连接成一个连通图,且所有连接了的边中权值最大的边权值最小. 解法:水题,直接用Kruskal算法做一遍就行 ...
- POJ 1861 ——Network——————【最小瓶颈生成树】
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15268 Accepted: 5987 Specia ...
- 最小生成树之 prim算法和kruskal算法(以 hdu 1863为例)
最小生成树的性质 MST性质:设G = (V,E)是连通带权图,U是V的真子集.如果(u,v)∈E,且u∈U,v∈V-U,且在所有这样的边中, (u,v)的权c[u][v]最小,那么一定存在G的一棵最 ...
- 最小生成树之Prim算法和Kruskal算法
最小生成树算法 一个连通图可能有多棵生成树,而最小生成树是一副连通加权无向图中一颗权值最小的生成树,它可以根据Prim算法和Kruskal算法得出,这两个算法分别从点和边的角度来解决. Prim算法 ...
- java实现最小生成树的prim算法和kruskal算法
在边赋权图中,权值总和最小的生成树称为最小生成树.构造最小生成树有两种算法,分别是prim算法和kruskal算法.在边赋权图中,如下图所示: 在上述赋权图中,可以看到图的顶点编号和顶点之间邻接边的权 ...
- 【数据结构】最小生成树之prim算法和kruskal算法
在日常生活中解决问题经常需要考虑最优的问题,而最小生成树就是其中的一种.看了很多博客,先总结如下,只需要您20分钟的时间,就能完全理解. 比如:有四个村庄要修四条路,让村子能两两联系起来,这时就有最优 ...
随机推荐
- c# datagridviewcomboboxcell值无效的解决办法
一直认为是数据库存储的数据和datagridviewcomboboxcell对不上导致,今天碰到两者对应上了,预览的时候还是提示错误, 查看了下网上其他大神的解决方法,是数据库字段类型有误,查看了下, ...
- Python相关项目和技术
下面的项目是<Learn PYTHON the hard way>里面的,以后可能会补充: 1.Django,创建web程序的框架:https://www.djangoproject.co ...
- randn命令中randn('state')和randn('seed')的不同
(1)RANDN产生正态分布数的语法: RANDN(N) :产生N× N的矩阵,其元素是按正态分布的数组: RANDN(M,N) and RANDN([M,N]):产生M×N的矩阵: RANDN ...
- ASP.net 学习路线(详细)
.net学习路线 入门篇1. 学习面向对象(OOP)的编程思想 许多高级语言都是面向对象的编程,.NET也不例外.如果您第一次接触面向对象的编程,就必须理解类.对象.字段.属性.方法和 ...
- reStructuredText(.rst)语法规则快速入门
原文:http://blog.useasp.net/archive/2014/09/05/rst-file-restructuredtext-markup-syntax-quikstart.aspx? ...
- Python之路 Day12
day12主要内容:html基础.CSS基础 HTML HTML概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标 ...
- C#实现时间戳转化
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...
- ORACLE存储过程笔记2
ORACLE存储过程笔记2 运算符和表达式 关系运算 =等于<>,!=不等于<小于>大于<=小于等于>=大于等于 一般运算 +加-减*乘/除 ...
- windows下安装ruby和 rails的痛苦经历
准备安装ruby on rails,在网上搜了下,步骤都类似,但实际安装过程中却碰到很多问题.下面详细说下: 说明下,文章是按照我尝试的过程描述的.但最终是靠 运行 railsinstaller一键式 ...
- BT基础知识简介
1. 蓝牙概述 无线局域网的通信 适用范围:10米到100米(根据发射功率的class不同有所差别,典型的class2为10m,而class1为100m,class3为1m) 应用: 局域网络 ...