Network

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 14721 Accepted: 5777 Special Judge

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

一道简单的kruskal算法,不过POJ上的样例有错,补一个样例;

样例输入:

5 8

1 2 5

1 4 2

1 5 1

2 3 6

2 4 3

3 4 5

3 5 4

4 5 6

样例输出:

4

4

1 5

1 4

2 4

3 5

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#define WW freopen("a1.txt","w",stdout)
using namespace std;
const int MAX = 1100;
struct Edge
{
int u;
int v;
int w;
} L[MAX*15];
int pre[MAX];
int a[MAX];
int n,m;
int sum,num,top;
bool cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int Find(int x)//并查集;
{
int i=x,j=x,s;
while(pre[i]!=i)
{
i=pre[i];
}
while(pre[j]!=i)
{
s=pre[j];
pre[j]=i;
j=s;
}
return i;
}
void Kruskal()
{
sum=0;
num=0;
top=0;
for(int i=0; i<m; i++)
{
int Fx=Find(L[i].u);
int Fy=Find(L[i].v);
if(Fy!=Fx)
{
if(sum<L[i].w)
{
sum=L[i].w;
}
a[top++]=i;
num++;
pre[Fx]=Fy;
}
if(num==n-1)
{
break;
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=1; i<=n; i++)
{
pre[i]=i;
}
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&L[i].u,&L[i].v,&L[i].w);
}
sort(L,L+m,cmp);
Kruskal();
printf("%d\n",sum);
printf("%d\n",num);
for(int i=0;i<top;i++)
{
printf("%d %d\n",L[a[i]].u,L[a[i]].v);
} }
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Network 分类: POJ 图论 2015-07-27 17:18 17人阅读 评论(0) 收藏的更多相关文章

  1. 【C#小知识】C#中一些易混淆概念总结(四)---------解析Console.WriteLine() 分类: C# 2014-02-05 17:18 1060人阅读 评论(0) 收藏

    目录: [C#小知识]C#中一些易混淆概念总结 [C#小知识]C#中一些易混淆概念总结(二) [C#小知识]C#中一些易混淆概念总结(三) ------------------------------ ...

  2. 修改MS SQL忽略大小写 分类: SQL Server 数据库 2015-06-19 09:18 34人阅读 评论(0) 收藏

    第一步:数据库->属性->选项->限制访问:SINGLE_USER 第二步:ALTER DATABASE [数据库名称] collate Chinese_PRC_CI_AI 第三步: ...

  3. 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...

  4. Matlab调用C程序 分类: Matlab c/c++ 2015-01-06 19:18 464人阅读 评论(0) 收藏

    Matlab是矩阵语言,如果运算可以用矩阵实现,其运算速度非常快.但若运算中涉及到大量循环,Matlab的速度令人难以忍受的.当必须使用for循环且找不到对应的矩阵运算来等效时,可以将耗时长的函数用C ...

  5. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  6. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  7. Speed Limit 分类: POJ 2015-06-09 17:47 9人阅读 评论(0) 收藏

    Speed Limit Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17967   Accepted: 12596 Des ...

  8. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  9. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

  10. Shuffle'm Up 分类: 函数 POJ 查找 2015-08-09 17:01 6人阅读 评论(0) 收藏

    Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7529 Accepted: 3466 Descript ...

随机推荐

  1. windows远程控制ubuntu尝试--未成功

    按照百度知道上的步骤一步一步操作,下载xrdp,一切很顺利. 直至出现了,如下的語言: connecting to sesman ip 尝试找到原因第一个原因 第二次的分析解释 至今还没找出原因

  2. SqlServer2008根据现有表,获取该表的分区创建脚本

    *============================================================== 名称: [GetMSSQLTableScript] 功能: 获取cust ...

  3. [Reprint]C++函数前和函数后加const修饰符区别

    c++中关于const的用法有很多,const既可以修饰变量,也可以函数,不同的环境下,是有不同的含义.今天来讲讲const加在函数前和函数后面的区别.比如: 01 #include<iostr ...

  4. Python学习总结19:类(一)

    在Python中,可以通过class关键字定义自己的类,通过类私有方法“__init__”进行初始化.可以通过自定义的类对象类创建实例对象. class Student(object): count ...

  5. MyEclipse下如何安装svn插件

    方法一:在线安装 1.打开HELP->MyEclipse Configuration    不过多数情况下再myeclipse实现不了   Center.切换到SoftWare标签页. 2.点击 ...

  6. 夺命雷公狗ThinkPHP项目之----企业网站16之文章批量删除的完成

    我们在做一个网站时候经常会遇到批量删除这个选项,其实也很简单,我们之需要用用jq实现出来效果然后通过表单post过去后端即可实现: 我们做这个功能前必须要先引入jq,我的jq版本是1.8.3,方法如下 ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON FillUp1

    zw版[转发·台湾nvp系列Delphi例程]HALCON FillUp1 procedure TForm1.Button1Click(Sender: TObject);var img : HImag ...

  8. 【linux】自定义配置debian+openbox

    openbox确实是好东西阿,小巧,不开iceweasel时内存总使用量不到200M,功能又强大,不过限于我是菜鸟,openbox再强大也有限...debian装openbox是及其简单的,不过为了使 ...

  9. android 学习随笔十八(广播与服务 )

    1.广播接收者注册 清单文件注册(Android四大组件都要在清单文件中注册) 一旦应用部署,广播接收者就生效了,直到用户手动停止应用或者应用被删除 广播接收者可以使用代码注册 需要广播接收者运行时, ...

  10. 鸟哥的linux私房菜学习记录之正则表达式

    正则表达式具有强大的字符串处理能力,常常用来搜索删除和替换字符串,用途很广. sed awk数据处理工具 diff,cmp,patch,pr文档对比工具