题目连接:

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算法的更多相关文章

  1. 【数据结构】 最小生成树(四)——利用kruskal算法搞定例题×3+变形+一道大水题

    在这一专辑(最小生成树)中的上一期讲到了prim算法,但是prim算法比较难懂,为了避免看不懂,就先用kruskal算法写题吧,下面将会将三道例题,加一道变形,以及一道大水题,水到不用高级数据结构,建 ...

  2. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  3. POJ 1861 Network (模版kruskal算法)

    Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...

  4. POJ 1861 Network

    题意:有n个点,部分点之间可以连接无向边,每条可以连接的边都有一个权值.求一种连接方法将这些点连接成一个连通图,且所有连接了的边中权值最大的边权值最小. 解法:水题,直接用Kruskal算法做一遍就行 ...

  5. POJ 1861 ——Network——————【最小瓶颈生成树】

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15268   Accepted: 5987   Specia ...

  6. 最小生成树之 prim算法和kruskal算法(以 hdu 1863为例)

    最小生成树的性质 MST性质:设G = (V,E)是连通带权图,U是V的真子集.如果(u,v)∈E,且u∈U,v∈V-U,且在所有这样的边中, (u,v)的权c[u][v]最小,那么一定存在G的一棵最 ...

  7. 最小生成树之Prim算法和Kruskal算法

    最小生成树算法 一个连通图可能有多棵生成树,而最小生成树是一副连通加权无向图中一颗权值最小的生成树,它可以根据Prim算法和Kruskal算法得出,这两个算法分别从点和边的角度来解决. Prim算法 ...

  8. java实现最小生成树的prim算法和kruskal算法

    在边赋权图中,权值总和最小的生成树称为最小生成树.构造最小生成树有两种算法,分别是prim算法和kruskal算法.在边赋权图中,如下图所示: 在上述赋权图中,可以看到图的顶点编号和顶点之间邻接边的权 ...

  9. 【数据结构】最小生成树之prim算法和kruskal算法

    在日常生活中解决问题经常需要考虑最优的问题,而最小生成树就是其中的一种.看了很多博客,先总结如下,只需要您20分钟的时间,就能完全理解. 比如:有四个村庄要修四条路,让村子能两两联系起来,这时就有最优 ...

随机推荐

  1. HDU 1969(二分法)

    My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N ...

  2. word排版的一些小技巧积累

    先准备好样式 编辑前,可以先根据要求,设置好样式,可以免去编辑好后,再修改格式(这样要改好多文本的格式) docx doc的样式不能通用. .docx转.doc 从word2013自带的编辑公式,编辑 ...

  3. mysql版sql助记

    新建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; [host 中 使用 % 为通配符, 匹配任意远程主机] 赋权 GRANT p ...

  4. USACO Palindromic Squares 【STL__string_的应用】

    这里有个讲解 string 用法非常详细的博文:https://www.byvoid.com/zhs/blog/cpp-string 题目意思很简单啦,就是找回文 使用string可以高速A过 Sou ...

  5. iOS中NSString转换成HEX(十六进制)-NSData转换成int

    http://www.2cto.com/kf/201402/281501.html 1 2 3 4 5 6 NSString *str = @"0xff055008"; //先以1 ...

  6. 【Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款/蓝绿色】玛萨玛索男装网购商城

    [Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款/蓝绿色]玛萨玛索男装网购商城 [特价商品] Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款 ...

  7. C++自增和自减运算符(--和++)

    在C和C++中,常在表达式中使用自增(++)和自减(--)运算符,他们的作用是使变量的值增1或减1,如:++i(在使用i之前,先使i的值加1,如果i的原值为3,则执行j=++i后,j的值为4)--i ...

  8. WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇]

    原文:WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇] 在[上篇]中,我们分别站在消息交换和编程的角度介绍了SOAP Fault和FaultException异常.在服务执行过 ...

  9. JVM调优总结(十二)-参考资料

    能整理出上面一些东西,也是因为站在巨人的肩上.下面是一些参考资料,供大家学习,大家有更好的,可以继续完善:) · Java 理论与实践: 垃圾收集简史 · Java SE 6 HotSpot[tm] ...

  10. shell登录模式及其相应配置文件(转)

    参考<linux命令.编辑器与shell编程>(清华大学出版社) 当启动shell时,它将运行启动文件来初始化自己.具体运行哪个文件取决于该shell是登陆shell还是非登陆shell的 ...