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肯定 ...
随机推荐
- jquery(select)下拉框 选取选中的值
var get_date_type=$("#date_type").find("option:selected").val(); var get_date_ty ...
- css之选择器总结
首先我们来看下有哪些选择器??? 一.基础选择器: html标签选择器:通过html标签来选择元素. ①所有的html标签都可以当做选择器. ②无论标签藏多深都会被选中. ③选择的是所有的标签而不是某 ...
- [leetcode-593-Valid Square]
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- JavaScript开发者必备的10个sublime的插件
http://www.codeceo.com/article/10-js-sublime-text-plugins.html
- create groups 和 create folder reference
当将文件拖入工程中的时候会出现这个对话框,这个对话框中在Added folders中有两种选择:Create groups 和 Create folder references 这两种的区别是 ...
- JS初步学习
[使用JS的三种方式] 1.HTML标签中内嵌JS(不提倡使用): <button onclick="javascript:alert('小碧池!你真点啊!')">有本 ...
- Chrome控制台使用详解
Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更快「开发调试」更强「进阶级的Frontender」 Bug无处遁形「 ...
- 前端解读Webview
作为盛行已久的开发方式,Hybrid的相关介绍已经是相当普遍了.不过看到博客园里基本上都是从android或者ios的角度来讲解的,对于h5的前端来说看起来只能是一直半解.感觉有必要从前端的角度来理解 ...
- 消耗CPU的程序
昨天领导交代客户需要一个可以测试CPU性能的脚本,问题简化下就是说要做一个可以手动设置对CPU产生消耗的程序.心想哪有这种脚本,或许性能测试工具还差不多.琢磨了下,或许用死循环可以达到差不多的效果,但 ...
- Elasticsearch学习随笔(二)-- Index 和 Doc 查询新建API总结
本文着重总结Elasticsearch的常见API了,进行分析. Index API 初始化Index,设置shards和replica PUT http://localhost:9200/firew ...