题目链接:

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

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

Northeastern Europe 2001, Northern Subregion

题意:

有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 &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. 数据挖掘算法之关联规则挖掘(一)apriori算法

    关联规则挖掘算法在生活中的应用处处可见,几乎在各个电子商务网站上都可以看到其应用 举个简单的例子 如当当网,在你浏览一本书的时候,可以在页面中看到一些套餐推荐,本书+有关系的书1+有关系的书2+... ...

  2. [Javascript] Conditionally spread entries to a JavaScript object

    In JavaScript, we often end up composing one object out of several other objects. Luckily there's a ...

  3. EJB学习笔记之十(BMT事务和CMT事务)

     1.前言 前两篇博客主要介绍了与事务相关的知识.比如事务的一些特性,以及并发产生的问题.本篇来解说一下EJB中两种处理事务的方式.一种是以生命式方式来管理事务(CMT):还有一种则是在EJB内部使用 ...

  4. Django——如何在Django模板中注入全局变量?——part2

    模版中的变量由context中的值来替换,如果在多个页面模版中含有相同的变量,比如:每个页面都需要{{user}},笨办法就是在每个页面的请求视图中都把user放到context中.   from d ...

  5. usb转串口模块下载时遇到的问题

    ch340g usb转TTL模块,烧写wifi模块ESP8266固件时,为图省事,我直接用的该模块的3.3v电为wifi模块供的电,结果刚一上电就出现串口模块消失(听到噔的一声),电脑设备管理器里就看 ...

  6. Python技术公众号100天了

    公众号100天了,是个值得一提的日子! 我从2017年10月31日开始做这个公众号,到今天2018年2月7日,差不多100天时间 .虽然公众号很早就申请了,但直到去年10月31日,我才有真正把这个公众 ...

  7. PV、UV、GMV

    原文地址:电商术语:客单价.UV.PV.转化率.销售额作者:馨闻莲博 UV(独立访客):Unique Visitor,访问您网站的一台电脑客户端为一个访客.00:00-24:00内相同的客户端只会被计 ...

  8. WebApi异常处理解决方案

    一.使用异常筛选器捕获所有异常 首先在App_Start里面新建一个类WebApiExceptionFilterAttribute.cs,继承ExceptionFilterAttribute,重写On ...

  9. nodejs 命令行获取入参

    安装:npm install yargs --save-dev Example index.js const argv = yargs.alias('n', 'name').alias('p', 'p ...

  10. Centos7使用LVM扩容磁盘(测试成功)

    1.新增加了一块200G大小的磁盘/dev/sdb fdisk -l 2. pvcreate /dev/sdb 3. pvdisplay 查看添加成功的/dev/sdb的大小为200G 4. vgex ...