POJ-1861-NETWORK 解题报告
| Time Limit: 1000MS | Memory Limit: 30000K | |||
| Total Submissions: 16628 | Accepted: 6597 | Special Judge | ||
Description
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
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
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 解题报告的更多相关文章
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- 【原创】poj ----- 1182 食物链 解题报告
题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- POJ 1861 ——Network——————【最小瓶颈生成树】
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15268 Accepted: 5987 Specia ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- poj 2051.Argus 解题报告
题目链接:http://poj.org/problem?id=2051 题目意思:题目有点难理解,所以结合这幅图来说吧---- 有一个叫Argus的系统,该系统支持一个 Register 命令,输入就 ...
- poj 1102.LC-Display 解题报告
题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...
- poj 1363 Rails 解题报告
题目链接:http://poj.org/problem?id=1363 题意:有一列火车,车厢编号为1-n,从A方向进站,向B方向出站.现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理. 实 ...
- POJ 1840 Eps 解题报告(哈希)
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,xi∈[-50,50],且xi!=0.让我们求所有解的可能. 首先,如果暴力判断的话,每个x的取值有100种可能,100^5肯定 ...
随机推荐
- web开发中前后端传值
在JavaScript中,页面与页面间的传值需要注意. 比如,我们通过url向下个页面进行传一个数字时,到下个页面进行解析出来后可能是一个字符串.这样会导致一个现象.调试时,发现我要传的值的确传过来了 ...
- 流畅的python学习笔记第七章:装饰器
装饰器就如名字一样,对某样事物进行装饰过后然后返回一个新的事物.就好比一个毛坯房,经过装修后,变成了精装房,但是房子还是同样的房子,但是模样变了. 我们首先来看一个函数.加入我要求出函数的运行时间.一 ...
- thinkphp之获取客户端IP地址
/** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @return mixed */ function get_cl ...
- C++ 获取文件夹下的所有文件名
获取文件夹下所有的文件名是常用的功能,今天再一次有这样的需求,所有就在网上查找了很多,并记下以供后用. 原文:http://blog.csdn.NET/cxf7394373/article/detai ...
- angular自动化测试--protractor
前戏 面向模型编程: 测试驱动开发: 先保障交互逻辑,再调整细节.---by 雪狼. 为什么要自动化测试? 1,提高产出质量. 2,减少重构时的痛.反正我最近重构多了,痛苦经历多了. 3,便于新人接手 ...
- window.getSelection和document.selection
window.getSelection和document.selection IE9以下支持:document.selection IE9.Firefox.Safari.Chrome和Opera支 ...
- Java WebService学习资料
最近用到了WebService,以前没用过,想要好好学习一下.感觉网上资料比较少,而且很杂,找了很久,觉得下面的两个文章解释的比较清楚,分享一下: WebService概念.原理:http://mp. ...
- 推荐系统那点事 —— 基于Spark MLlib的特征选择
在机器学习中,一般都会按照下面几个步骤:特征提取.数据预处理.特征选择.模型训练.检验优化.那么特征的选择就很关键了,一般模型最后效果的好坏往往都是跟特征的选择有关系的,因为模型本身的参数并没有太多优 ...
- 关于Calendar中设置月份比实际小1的问题
有如下程序,转化两个字符串数字为date类型,并判断是历史上的星期几,是否同为星期一 代码如下: public static void main(String[] args) throws Parse ...
- 部分转载[C#性能优化实践]
全文出处:http://www.infoq.com/cn/articles/C-sharp-performance-optimization 1.性能 主要指两个方面:内存消耗和执行速度.性能优化简而 ...