POJ 1861:Network(最小生成树&&kruskal)
| Time Limit: 1000MS | Memory Limit: 30000K | |||
| Total Submissions: 13266 | Accepted: 5123 | 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
这题我就不吐槽了。
。
例子都是错的。。我没看讨论区。。
一直不知道。
。
浪费我那么多时间调试。
。
我去。
。
各种吐血。
。我也逗比。。
例子明显的出现环。。
正确的例子应该是:
1
3
1 2
1 3
3 4
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath> using namespace std; const int maxn1 = 15050;//边数的最大值
const int maxn2 = 1050;//顶点个数的最大值
int f[maxn2];//f[i]为顶点i在集合对树中的根节点
int s[maxn1];//记录选择的边的序号
int n, m;//集线器的个数。 边的个数
int cnt;//选择的边的数目
int ans;//记录最大的长度 struct Edge
{
int u;
int v;
int len;
};
Edge edge[maxn1];//边的数组 bool cmp(Edge a, Edge b)//按长度从小到大的排序
{
return a.len<b.len;
} void init()//初始化
{
for(int i=0; i<=n; i++)
f[i] = i;
} int find(int x)//并查集的find函数
{
return f[x] == x? x:f[x]=find( f[x] );
} void kruskal()
{
int x, y;
cnt = 0;
for(int i=1; i<=m; i++)
{
x = find( edge[i].u );
y = find( edge[i].v );
if( x==y ) continue;
f[y] = x;
ans = edge[i].len;
cnt++;
s[cnt] = i;
if( cnt>=n-1 ) break;
}
} void output()//输出函数
{
printf("%d\n", ans);
printf("%d\n", cnt);
for(int i=1; i<=cnt; i++)
printf("%d %d\n", edge[ s[i] ].u, edge[ s[i] ].v);
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
init();
for(int i=1; i<=m; i++)
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].len);
sort( edge+1, edge+m+1, cmp );
kruskal();
output();
} return 0;
}
POJ 1861:Network(最小生成树&&kruskal)的更多相关文章
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
- POJ 1861 Network
题意:有n个点,部分点之间可以连接无向边,每条可以连接的边都有一个权值.求一种连接方法将这些点连接成一个连通图,且所有连接了的边中权值最大的边权值最小. 解法:水题,直接用Kruskal算法做一遍就行 ...
- POJ 1861 ——Network——————【最小瓶颈生成树】
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15268 Accepted: 5987 Specia ...
- POJ 1861 Network (MST)
题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. //#include <bits/stdc++.h> #includ ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
随机推荐
- 【WEB API项目实战干货系列】- WEB API入门(一)
这篇做为这个系列的第一篇,做基本的介绍,有经验的人可以直接跳到第二部分创建 ProductController. 创建 Web API 项目 在这里我们使用VS2013, .NET 4.5.1创建 ...
- Cordova cannot add Android failed with exit code ENOENT
这可能是系统环境变量损坏了 解决方案:在系统变量path如果没用下面的变量就加上%SystemRoot%\system32; %SystemRoot%; %SystemRoot%\System32\W ...
- abstract的方法是否可同时是static,是否可同时是native,是否可同时是synchronized?
1.abstract与static (what) abstract:用来声明抽象方法,抽象方法没有方法体,不能被直接调用,必须在子类overriding后才能使用 static:用来声明静态方法,静态 ...
- 老男孩Python全栈开发(92天全)视频教程 自学笔记02
day2课程目录: 操作系统的历史 二进制运算 字符编码与计算机容量 编程语言介绍 开发语言分类 演讲能力锻炼 day2课程内容梳理: 操作系统发展史: Unix系统 极其稳定 现在很多银行还在用 L ...
- [特斯拉组件]ios高性能PageController
本文来自于腾讯Bugly公众号(weixinBugly),作者:sparrowchen,未经作者同意,请勿转载,原文地址: http://mp.weixin.qq.com/s/hBgvPBP12IQ1 ...
- 编程语言的基础——搞定JavaIO
关键字:IO基础,JUnit生命周期,字节流,字符流,字符编码,对象流,序列化,反序列化 Java I/O 流是一组有顺序的,有起点和终点的字节集合.是对设备文件间数据传输的总称和抽象. 在IO中涉及 ...
- [C#]使用GroupJoin将两个关联的集合进行分组
本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 本文使用的开发环境是VS2017及dotNet4.0,写此随笔的目的是给自己及新开发人员作为参 ...
- UITextField的使用小技巧
[tf setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];//修改placeHolder ...
- warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
------问题-------------------- Qt项目使用 VC++ 编译器出现此错误. warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 ...
- Linux 链接详解----静态链接实例分析
由Linux链接详解(1)中我们简单的分析了静态库的引用解析和重定位的内容, 下面我们结合实例来看一下静态链接重定位过程. /* * a.c */ ; void add(int c); int mai ...