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肯定 ...
随机推荐
- 《JavaScript高级程序设计》笔记一
第一章 JavaScript简介 一.JavaScript的起源 JavaScript诞生于1995年.当时,它的主要作用是处理一些输入验证操作.之前的话,都是把表单数据发送到服务器端,然后再去判断有 ...
- Python对数据库的增删改查
#!/usr/bin/env python import MySQLdb DATABASE_NAME = 'hero' class HeroDB: # init class and ...
- 6.javaweb之respose对象
1.respose的生成的outer对象要优先于内置的out对象输出 response.setContentType("text/html;charaset=utf-8");//设 ...
- Python3中文件处理
1.txt,xls,doc等文件的使用 f=open("filename","w") 打开一个用于写入的文件,要写入内容时使用f.write("内 ...
- ReadAndWriteBinaryFile
package JBJADV003;import java.io.FileInputStream;import java.io.DataInputStream;import java.io.EOFEx ...
- PostMessage,模拟键盘输入
for i := 0 to length(tel) do begin //SendMessage(cHwnd,WM_KEYDOWN,Integer(tel[ ...
- JavaScript一个cookie存储的类
所有输出都在浏览器的控制台中 <script type="text/javascript"> /** * cookieStorage.js * 本类实现像localSt ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A B题
当时晚上打CF时候比较晚,加上是集训期间的室友都没有晚上刷题的习惯,感觉这场CF很不在状态.A题写复杂WA了一发后去厕所洗了个脸冷静了下,换个简单写法,可是用cin加了ios::sync_with_s ...
- C# 汉语转拼音
汉语转拼音或首字母 通常不少网站上有汉语转拼音功能,今天就小记下这段汉语转拼音的代码,自己测试ok,现把代码贴出来,以备日后使用: 效果 用法很简单后台使用到了两个类文件,一个是:ConvertHzT ...
- 如是使用JS实现页面内容随机显示
之前有个客户咨询我,因为他们公司的业务员有多个人,但公司网站的联系方式板块里只够放一个人的信息,所以就想能不能实现这个联系方式信息随机显示,对于业务或客服人员来说也能做到分配均匀公平.本文我们将和大家 ...