Network
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16628   Accepted: 6597   Special Judge

Description

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 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 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

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

Source

Northeastern Europe 2001, Northern Subregion

好久没有更新这个博客,觉得自己还是不够努力,自己的acm学习之路的脚步走的很慢,希望尽快改变现状吧。这道题考察kruskal算法,留下个记录

题解:题目大意是有n个顶点,m条边,使图连通,使边权和最小,不构成回路

kruskal算法:按边权从小到到排序,顺序将边加入图中,若图中两点不连通则加入,否则就不考虑,(判断两点是否在图中中并查集,并查集的主要功能就是合并和查找的功能),最终就可以解答此题,找到最小的边权的连通图。

初始化并查集f[]数组(f中每个数等于下标值)find(x),

查找x的祖先,若x!=find(x)则find(find(x)),递归操作即可

合并操作,union(x,y) 若两个是不同的祖先则合并。

#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn = 1e6+7; int father[maxn]; int n, m, c = 1;
struct _edge{
int x, y, d;
}edge[maxn], ans[maxn]; bool cmp(_edge a, _edge b)
{
return a.d < b.d;
} int fi(int x)
{
return x == father[x] ? x : father[x] = fi(father[x]);
} void union1(int x, int y)
{
int p1 = fi(x), p2 = fi(y);
if (p1 == p2) return;
father[p1] = p2;
} int check(int x, int y)
{
int p = fi(x), q = fi(y);
if (p == q)
return 1;
return 0;
} void init()
{
for (int i=0; i<=n; i++)
father[i] = i;
} void kruskal()
{
for (int i=1; i<=m; i++)
{
int x = edge[i].x;
int y = edge[i].y;
if (fi(x) != fi(y)) {
union1(x, y);
ans[c].x = x;
ans[c].y = y;
ans[c].d = edge[i].d;
c++;
}
}
} int main()
{
int x, y;
scanf("%d%d", &n, &m);
init();
for (int i=1; i<=m; i++)
{
scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].d);
}
sort(edge+1, edge+m+1, cmp);
kruskal();
printf("%d\n", ans[c-1].d);
printf("%d\n", c-1);
for (int i=1; i<c; i++) {
printf("%d %d\n", ans[i].x, ans[i].y);
}
return 0;
}

POJ-1861-NETWORK 解题报告的更多相关文章

  1. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  2. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  3. POJ 1861 Network (Kruskal求MST模板题)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Specia ...

  4. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

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

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

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

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

  7. poj 2051.Argus 解题报告

    题目链接:http://poj.org/problem?id=2051 题目意思:题目有点难理解,所以结合这幅图来说吧---- 有一个叫Argus的系统,该系统支持一个 Register 命令,输入就 ...

  8. poj 1102.LC-Display 解题报告

    题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...

  9. poj 1363 Rails 解题报告

    题目链接:http://poj.org/problem?id=1363 题意:有一列火车,车厢编号为1-n,从A方向进站,向B方向出站.现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理. 实 ...

  10. POJ 1840 Eps 解题报告(哈希)

    a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,xi∈[-50,50],且xi!=0.让我们求所有解的可能. 首先,如果暴力判断的话,每个x的取值有100种可能,100^5肯定 ...

随机推荐

  1. PHP中几个输出函数echo,print(),print_r(),sprintf(),var_dump()的区别

    1:echo:是语句不是函数,没有返回值,可输出多个变量值,不需要圆括号.不能输出数组和对象,只能打印简单类型(如int,string). 2:print:是语句不是函数,有返回值 1 ,只能输出一个 ...

  2. thinkphp带查询条件的分页

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. 类间调用inline函数的效率

    问题描述: class A { public: int x, y, k, NY; inline int f(int i, int j, int k)  {return ((i)*(NY + 1) * ...

  4. Chrome控制台使用详解

    Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更快「开发调试」更强「进阶级的Frontender」 Bug无处遁形「 ...

  5. 定制Android开发者专属T恤

    之前在T社上买了一件定制的T恤,感觉质量挺不错的,那是段子张发起的众筹.正面有hello google这几个字母. 我自己本身是一个Android粉,从nexus手机到pixel手机,坚持买原生的操作 ...

  6. Web office apps 安装部署

    系统要求为Windows Server 2012, 注意:安装Office Web Apps的服务器除了Office Web Apps之外,不能安装其他应用.包括不能安装Office,lync,,sh ...

  7. Jenkins 配置邮件通知

    jenkins 是一个开源的自动化服务器.通过Jenkins,可以通过自动化加速软件开发过程.Jenkins管理和控制各种开发的生命周期过程,包括构建,文档,测试,包,阶段,部署,静态分析等等.您可以 ...

  8. 登录界面Demo

    今天记载一个Demo,这个是我练习项目中用到,供新手看看,界面图:

  9. electron入门代码

    Electron 提供了一个实时构建桌面应用的纯 JavaScript 环境.Electron 可以获取到你定义在 package.json 中 main 文件内容,然后执行它.通过这个文件(通常我们 ...

  10. 设计模式--单例模式(Singleton pattern)及应用

    单例模式 参考文档: 该文仅介绍spring的单例模式:spring 的单例模式 介绍原理:Spring的单例模式底层实现 参考书籍:漫谈设计模式:从面向对象开始-刘济华.pdf 1. 单例模式解析 ...