题目链接:

PKU:http://poj.org/problem?id=1861

ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=542

Description

Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each 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

The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered 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

Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding
cable. Separate numbers by spaces and/or line breaks.

Sample Input

  1. 4 6
  2. 1 2 1
  3. 1 3 1
  4. 1 4 2
  5. 2 3 1
  6. 3 4 1
  7. 2 4 1

Sample Output

  1. 1
  2. 4
  3. 1 2
  4. 1 3
  5. 2 3
  6. 3 4

Source

Northeastern Europe 2001, Northern Subregion

题意:

有n个顶点,m条边,每条边都是双向的,而且有一定的长度。要求使每一个顶点都连通,而且要使总长度最短,

输出最大边、边的总数和所选择的边。

PS:

貌似题目的案例有点问题,卡了好久!

应该输出的是:

1

3

1 3

2 3

2 4

代码例如以下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. const int maxn = 15017;
  6. int father[maxn];
  7. struct edge
  8. {
  9. int x,y,v;
  10. };
  11. struct edge ed[maxn],ansa[maxn];
  12.  
  13. bool cmp(edge a,edge b)
  14. {
  15. return a.v<b.v;
  16. }
  17.  
  18. int find(int x)
  19. {
  20. if(x==father[x])
  21. return x;
  22. return father[x]=find(father[x]);
  23. }
  24.  
  25. void Krusal(int n,int m)
  26. {
  27. int i,fx,fy,cnt;
  28. int ans=0;
  29. for(i = 1; i <= n; i++)
  30. father[i]=i;
  31. sort(ed,ed+m,cmp);//对边的排序
  32. cnt=0;
  33. int max=-1;
  34. for(i=0; i<m; i++)
  35. {
  36. fx=find(ed[i].x);
  37. fy=find(ed[i].y);
  38. if(fx!=fy)
  39. {
  40. ans+=ed[i].v;
  41. father[fx]=fy;
  42. ansa[cnt].x=ed[i].x;
  43. ansa[cnt++].y=ed[i].y;
  44. if(max<ed[i].v)
  45. max=ed[i].v;
  46. }
  47. }
  48. printf("%d\n%d\n",max,cnt);
  49. for(i=0; i<cnt; i++)
  50. printf("%d %d\n",ansa[i].x,ansa[i].y);
  51. }
  52.  
  53. int main()
  54. {
  55. int t;
  56. int n, m;
  57. int a, b, k;
  58.  
  59. while(scanf("%d %d",&n,&m)!=EOF)
  60. {
  61. for(int i = 0; i < m; i++)
  62. {
  63. scanf("%d %d %d",&a,&b,&k);
  64. ed[i].x=a,ed[i].y=b,ed[i].v=k;
  65. }
  66. Krusal(n,m);
  67. }
  68. return 0;
  69. }

POJ 1861 &amp; ZOJ 1542 Network(最小生成树之Krusal)的更多相关文章

  1. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  2. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  3. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  4. POJ 1861:Network(最小生成树&amp;&amp;kruskal)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13266   Accepted: 5123   Specia ...

  5. POJ 2349 Arctic Network (最小生成树)

    Arctic Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  6. POJ 1861 Network (Kruskal求MST模板题)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Specia ...

  7. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  8. POJ 1861 Network (模版kruskal算法)

    Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...

  9. POJ 2349 Arctic Network(最小生成树+求第k大边)

    题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...

随机推荐

  1. RocketMQ性能压测分析(转)

    原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2093785 一   机器部署 1.1  机器组成 1台nameserver 1台broker  异步刷盘 2 ...

  2. struts2入门示例(hello world)

    1. 环境搭建 按照之前的文章配置好myeclipse的jdk和tomcat,并新建一个web项目后,可开始动手配置与struts2相关的地方了.首先去struts的官网下载好最新的struts2代码 ...

  3. 《深入浅出mfc》 第1章 笔记

    需要什么函数库(.lib) windows支持动态链接库,应用程序所调用 的windows api 函数是在“执行期间“才链接上的.Windows程序调用 可以分为 C Runtimes以及windo ...

  4. python的threading和multiprocessing模块初探

    转载于:http://blog.csdn.net/zhaozhi406/article/details/8137670

  5. redis内存分析(转)

    背景 线上经常遇到用户想知道自己 Redis 实例中数据的内存分布情况.为了不影响线上实例的使用,我们一般会采用 bgsave 生成 dump.rdb 文件,再结合 redis-rdb-tools 和 ...

  6. Linux中如何配置IP

      与网络相关的文件:1) /etc/sysconfig/network   设置主机名称及能否启动Network2) /etc/sysconfig/network-scripts/ifcfg-eth ...

  7. 联想电脑Win8升级win10后Wlan关闭无法开启解决办法

    官网下载电源驱动,下载无线网上驱动 开启电脑 按fn+f5 电源管理界面就出来了 把无线网卡打开 就ok了 这样就开启了无线! 如果还不行,可进行如下尝试,希望有所帮助: 1.开机进bios(一般是按 ...

  8. Atitit.php opcode虚拟机指令集 分类以及详细解释

    Atitit.php opcode虚拟机指令集 分类以及详细解释 1. 指令集常用分类:: Mov移动指令 算数逻辑移位指令 跳转指令 Oo指令 类型转换指令 2. 与jvm  clr指令集合对比 P ...

  9. 转:SQL2008 UNPIVOT 列转行示例

    CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int); GO INSERT INTO pv ...

  10. C# 获取或设置本地打印机及配置文件操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...