POJ 1861 Network (Kruskal求MST模板题)
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 14103 | Accepted: 5528 | Special Judge |
Description
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
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
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
题目链接:poj.org/problem?
id=1861
题目大意:n个点,m条线,每条线有个权值,如今要求最长的路最短且让各个点都连通,求最短的最长路,边个数和相应边
题目分析:例子有问题,应该是
1
4
1 2
1 3
3 4
裸的Kruskal注意这里要求最长路最短,而Kruskal正好是对权值从小到大排序后的贪心算法
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 15005;
int fa[MAX];
int n, m, ma, num;
int re1[MAX], re2[MAX]; struct Edge
{
int u, v, w;
}e[MAX]; bool cmp(Edge a, Edge b)
{
return a.w < b.w;
} void UF_set()
{
for(int i = 0; i < MAX; i++)
fa[i] = i;
} int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} void Union(int a, int b)
{
int r1 = Find(a);
int r2 = Find(b);
if(r1 != r2)
fa[r2] = r1;
} void Kruskal()
{
UF_set();
for(int i = 0; i < m; i++)
{
int u = e[i].u;
int v = e[i].v;
if(Find(u) != Find(v))
{
re1[num] = u;
re2[num] = v;
Union(u, v);
ma = max(ma, e[i].w);
num ++;
}
if(num >= n - 1)
break;
}
} int main()
{
ma = 0;
num = 0;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++)
scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
sort(e, e + m, cmp);
Kruskal();
printf("%d\n%d\n", ma, num);
for(int i = 0; i < num; i++)
printf("%d %d\n", re1[i], re2[i]);
}
POJ 1861 Network (Kruskal求MST模板题)的更多相关文章
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- POJ 1258 Agri-Net 【Prime】模板题
题目链接>>> 题目大意: 给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <c ...
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- POJ 1860 Currency Exchange【bellman-Ford模板题】
传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- [poj1144]Network(求割点模板)
解题关键:割点模板题. #include<cstdio> #include<cstring> #include<vector> #include<stack& ...
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
随机推荐
- pycharm永久激活(转载)
转载自CSDN--http://blog.csdn.net/mr_hhh/article/details/79062747 2018-02-2417:30:52 今天再补充一个教程,关于pycharm ...
- 自学php【一】 任务:图片上传即时可见
工作已经快2周了,头儿给派了个任务做个企业站!这几天正在紧锣密鼓的作战中!等忙完了这个活!写下自己的学习心得体会!与看到文章的您一起分享! 在这里记录每次遇到的难题,如何解决的! 今天要做的功能就是实 ...
- Flask框架 之路由
一.视图函数路由规则 from flask import Flask, redirect, url_for # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录 ...
- angular5中的自定义指令(属性指令)
属性型指令用于改变一个 DOM 元素的外观或行为. 在 Angular 中有三种类型的指令: 组件 — 拥有模板的指令 结构型指令 — 通过添加和移除 DOM 元素改变 DOM 布局的指令 属性型指令 ...
- 爬虫之Selenium库
官方文档:https://selenium-python.readthedocs.io/ Selenium:自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染的问题. 一.开始 ...
- mess系统 开发技术,需求整理
1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源(如html 页 ...
- python实现定时发送qq消息
因为生活中老是忘记各种事情,刚好又在学python,便突发奇想通过python实现提醒任务的功能(尽管TIM有定时功能),也可定时给好友.群.讨论组发送qq消息.其工作流程是:访问数据库提取最近计划— ...
- unigui+fastreport 打印【4】
1.建立一个uniForm,用于建立FastReport打印界面.在Form上增加uniFrame.和传统的的报表打印设计一样一样的. 2.在beofeShow事情中: procedure TUniF ...
- 【Lqb T336】Cowboys
[链接] 我是链接,点我呀:) [题意] 出现AB就要交换一下 给你结果序列 问你原序列有多少种可能 首尾可以交换. [题解] 设 dp[i][0]表示i和i-1不交换,达到前i个字符序列的状态的方案 ...
- [BZOJ1163&1339]Mafia
[Baltic2008]Mafia 题目 匪徒准备从一个车站转移毒品到另一个车站,警方准备进行布控. 对于每个车站进行布控都需要一定的代价,现在警方希望使用最小的代价控制一些车站,使得去掉这些车站后, ...