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个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...
随机推荐
- 马化腾从CFIDO到QQ(CFIDO BBS回忆录)
微博上看到一个和马化腾貌似挺熟的人,聊起了和马化腾的交往,偶然间提到了这个CFIDO东西,搜索了一下,发现是远古的一个bbs.然后还看到一篇以网友的视角写的当时的一些回忆.我觉得挺好玩的,然后看到文章 ...
- netty handlers模式
netty的handler模式真的挺方便的,可以像插件一样随意的插入自己新增的功能而不用队系统进行大的变动. 下面我们来看一下这个模式是如何实现和运行的. 待续...
- php核心技术与最佳实践知识点(下)
九.缓存 1.缓存三大要素:命中率, 缓存更新策略,缓存最大数据量 2.命中率(mysql为例):mysql提供了一系列的query cache的global status来提现数据库缓存的情况: s ...
- C 实现strcmp,strcpy,strcat函数
基于C语言的strcmp,strcpy,strcat函数的实现.C语言是一个程序猿的基础,一定要重视. char* strcat ( char * dst , const char * src ) { ...
- css选择器和xpath对照表
- GDB基本命令(整合)(转)
directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...
- Web前端开发实战1:二级下拉式菜单之CSS实现
二级下拉式菜单在各大学校站点.电商类站点.新闻类站点等大型?站点非经常见,那么它的实现原理是什么呢? 学习了Web前端开发的知识后,我们是能够实现这种功能的.复杂的都是从基础效果上加入做出来的.原理和 ...
- CentOS最常用命令
快捷键.常用命令: 文件和目录:# cd /home 进入 '/home' 目录# cd .. 返回上一级目录# cd ../.. 返回上两级目录# cd - 返回上次所在目录# cp file1 f ...
- ijkPlayer 集成
代码地址如下:http://www.demodashi.com/demo/11957.html 概述 ijkplayer 是一款做视频直播的框架,基于FFmpeg,支持Android和iOS.这里介绍 ...
- 【设计模式 7】从公司的目前框架和API Gateway,谈谈对外观模式的理解
我,第一次用到外观模式,应该是3年多以前.那时候是做一个收费系统,在当时的U层和B层之间,加了一层Facade.当时,在一些复杂的业务逻辑处理时,感受到了加入外观层的好处,但对于一些简单的(我指的是, ...