POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)
题目链接:
PKU:http://poj.org/problem?id=1861
ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=542
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
题意:
有n个顶点,m条边,每条边都是双向的,而且有一定的长度。要求使每一个顶点都连通,而且要使总长度最短,
输出最大边、边的总数和所选择的边。
PS:
貌似题目的案例有点问题,卡了好久!
应该输出的是:
1
3
1 3
2 3
2 4
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 15017;
int father[maxn];
struct edge
{
int x,y,v;
};
struct edge ed[maxn],ansa[maxn]; bool cmp(edge a,edge b)
{
return a.v<b.v;
} int find(int x)
{
if(x==father[x])
return x;
return father[x]=find(father[x]);
} void Krusal(int n,int m)
{
int i,fx,fy,cnt;
int ans=0;
for(i = 1; i <= n; i++)
father[i]=i;
sort(ed,ed+m,cmp);//对边的排序
cnt=0;
int max=-1;
for(i=0; i<m; i++)
{
fx=find(ed[i].x);
fy=find(ed[i].y);
if(fx!=fy)
{
ans+=ed[i].v;
father[fx]=fy;
ansa[cnt].x=ed[i].x;
ansa[cnt++].y=ed[i].y;
if(max<ed[i].v)
max=ed[i].v;
}
}
printf("%d\n%d\n",max,cnt);
for(i=0; i<cnt; i++)
printf("%d %d\n",ansa[i].x,ansa[i].y);
} int main()
{
int t;
int n, m;
int a, b, k; while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i = 0; i < m; i++)
{
scanf("%d %d %d",&a,&b,&k);
ed[i].x=a,ed[i].y=b,ed[i].v=k;
}
Krusal(n,m);
}
return 0;
}
POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)的更多相关文章
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- POJ 1861:Network(最小生成树&&kruskal)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13266 Accepted: 5123 Specia ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
- POJ 2349 Arctic Network(最小生成树+求第k大边)
题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...
随机推荐
- Scala 将BigDecimal转换为Long
待转换.asInstanceOf[Number].longValue (Double转为Long也适用)
- Python程序员的10个常见错误
关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...
- Django——如何在Django模板中注入全局变量?——part2
模版中的变量由context中的值来替换,如果在多个页面模版中含有相同的变量,比如:每个页面都需要{{user}},笨办法就是在每个页面的请求视图中都把user放到context中. from d ...
- 【Java】Java_01初步
1.编程语言的发展史和发展主线 计算机语言如果你将它当做一个产品,就像我们平时用的电视机.剃须刀.电脑.手机等, 他的发展也是有规律的. 任何一个产品的发展规律都是:向着人更加容易使用.功能越来越强大 ...
- <转>windows下编译lua源码
因为之前一直使用 lua for windows 来搭建lua的使用环境,但是最新的 lua for windows 还没有lua5.2,我又想用这个版本的lua,所以被逼无奈只能自己编一下lua源码 ...
- CSS3 not
AND (&&): .registration_form_right input:not([type="radio"]):not([type="check ...
- Centos RSA 登录
创建密钥 ssh-keygen -t rsa 1.用户目录下新建.ssh/authorized_keys mkdir .ssh #创建隐藏目录 #修改文件访问权限 chmod 700 .ssh cd ...
- Atitit.atijson 类库的新特性设计与实现 v3 q31
Atitit.atijson 类库的新特性设计与实现 v3 q31 1. V1版本---集成了多引擎1 2. V2版本新特性 --bsh脚本化2 3. V3版本新特性---循环引用解决使用fastjs ...
- Atitit。Tree文件解析器的原理流程与设计实现 java c# php js
Atitit.Tree文件解析器的原理流程与设计实现 java c# php js 1. 解析原理与流程1 1.1. 判断目录 ,表示服 dirFlagChar = "└├─&quo ...
- altera tcl
例子:https://www.altera.com/support/support-resources/design-examples/intellectual-property/embedded/n ...