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个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...
随机推荐
- RocketMQ性能压测分析(转)
原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2093785 一 机器部署 1.1 机器组成 1台nameserver 1台broker 异步刷盘 2 ...
- struts2入门示例(hello world)
1. 环境搭建 按照之前的文章配置好myeclipse的jdk和tomcat,并新建一个web项目后,可开始动手配置与struts2相关的地方了.首先去struts的官网下载好最新的struts2代码 ...
- 《深入浅出mfc》 第1章 笔记
需要什么函数库(.lib) windows支持动态链接库,应用程序所调用 的windows api 函数是在“执行期间“才链接上的.Windows程序调用 可以分为 C Runtimes以及windo ...
- python的threading和multiprocessing模块初探
转载于:http://blog.csdn.net/zhaozhi406/article/details/8137670
- redis内存分析(转)
背景 线上经常遇到用户想知道自己 Redis 实例中数据的内存分布情况.为了不影响线上实例的使用,我们一般会采用 bgsave 生成 dump.rdb 文件,再结合 redis-rdb-tools 和 ...
- Linux中如何配置IP
与网络相关的文件:1) /etc/sysconfig/network 设置主机名称及能否启动Network2) /etc/sysconfig/network-scripts/ifcfg-eth ...
- 联想电脑Win8升级win10后Wlan关闭无法开启解决办法
官网下载电源驱动,下载无线网上驱动 开启电脑 按fn+f5 电源管理界面就出来了 把无线网卡打开 就ok了 这样就开启了无线! 如果还不行,可进行如下尝试,希望有所帮助: 1.开机进bios(一般是按 ...
- Atitit.php opcode虚拟机指令集 分类以及详细解释
Atitit.php opcode虚拟机指令集 分类以及详细解释 1. 指令集常用分类:: Mov移动指令 算数逻辑移位指令 跳转指令 Oo指令 类型转换指令 2. 与jvm clr指令集合对比 P ...
- 转:SQL2008 UNPIVOT 列转行示例
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int); GO INSERT INTO pv ...
- C# 获取或设置本地打印机及配置文件操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...