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. SQLSERVER:sqlserver2008r2安装好后,自动提示功能不可以使用

    刚安装好的sqlserver2008r2x64,写一些sql时,自动提示功能失效了. 解决排查一: 找到tools->options->Text Editor->Transact-S ...

  2. [转] 国内外最全面和主流的JS框架与WEB UI库(强烈推荐)

    国内外最全面和主流的JS框架与WEB UI库...   当下对于网站前段开发人员来说,很少有人不使用一些JS框架或者WEB UI库,因此这些可以有效提高网站前段开发速度,并且能够统一开发环境,对于不同 ...

  3. 异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource

    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common ...

  4. linux挂接U盘

    挂接U盘fdisk -lDisk /dev/sdd: 131 MB, 131072000 bytes/dev/sdd1 * 1 889 127983+ b Win95 FAT32#mkdir -p / ...

  5. MVC4中下拉菜单和单选框的简单设计方法

    举例一: @Html.LabelFor(model => model.Gender) @Html.DropDownListFor(model => model.Gender, new[] ...

  6. shell 命令

  7. libsvm

    代码文件主要针对Matlab进行说明,但个人仍觉得讲解的支持向量机内容非常棒,可以做为理解这一统计方法的辅助资料; LibSVM是台湾林智仁(Chih-Jen Lin)教授2001年开发的一套支持向量 ...

  8. Openstack的镜像属性

    先来看张图: 容易理解的地方我们就不介绍了,我们这里介绍'公有'和'受保护'的 在shell命令中,公有用is-public=True表示,而受保护的用is-protected表示,公有的反面是is- ...

  9. SQL内连接与外连接的区别【转】

    --表stuid name 1, Jack2, Tom3, Kity4, nono--表examid grade1, 562, 7611, 89 内连接 (显示两表id匹配的)select stu.i ...

  10. 一个基于和围绕Docker生态环境构建的早期项目列表

    https://blog.docker.com/2013/07/docker-projects-from-the-docker-community/