Network 分类: POJ 图论 2015-07-27 17:18 17人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- 【C#小知识】C#中一些易混淆概念总结(四)---------解析Console.WriteLine() 分类: C# 2014-02-05 17:18 1060人阅读 评论(0) 收藏
目录: [C#小知识]C#中一些易混淆概念总结 [C#小知识]C#中一些易混淆概念总结(二) [C#小知识]C#中一些易混淆概念总结(三) ------------------------------ ...
- 修改MS SQL忽略大小写 分类: SQL Server 数据库 2015-06-19 09:18 34人阅读 评论(0) 收藏
第一步:数据库->属性->选项->限制访问:SINGLE_USER 第二步:ALTER DATABASE [数据库名称] collate Chinese_PRC_CI_AI 第三步: ...
- 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏
以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...
- Matlab调用C程序 分类: Matlab c/c++ 2015-01-06 19:18 464人阅读 评论(0) 收藏
Matlab是矩阵语言,如果运算可以用矩阵实现,其运算速度非常快.但若运算中涉及到大量循环,Matlab的速度令人难以忍受的.当必须使用for循环且找不到对应的矩阵运算来等效时,可以将耗时长的函数用C ...
- Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏
Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...
- 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 ...
- Speed Limit 分类: POJ 2015-06-09 17:47 9人阅读 评论(0) 收藏
Speed Limit Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17967 Accepted: 12596 Des ...
- 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 ...
- Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...
- 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 ...
随机推荐
- mongodb查询文档
说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...
- Tomcat类加载器机制
Tomcat为什么需要定制自己的ClassLoader: 1.定制特定的规则:隔离webapp,安全考虑,reload热插拔 2.缓存类 3.事先加载 要说Tomcat的Classloader机制,我 ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- struts 2.0部署
环境:linux centos 64位. 1)下载JDK6.0,具体文件名是:jdk-6u45-linux-x64.bin 安装:chmod 755 jdk*.bin ./jdk....bin 设置环 ...
- 在自定义的UINavigationController中设置背景图片
//这个方法中设置 + (void)initialize { UINavigationBar *bar = [UINavigationBar appearance]; [bar setBackgrou ...
- 将url转化成对象
<script> var url = "baidu.com?"; //var url = window.location.search; //获取url functio ...
- 位置式PID与增量式PID算法
位置式PID与增量式PID算法 PID控制是一个二阶线性控制器 定义:通过调整比例.积分和微分三项参数,使得大多数的工业控制系统获得良好的闭环控制性能. 优点 ...
- paper 52 :windows7环境下theano安装
要做卷积神经网络的一些东西,所以要装theano,网上很多Theano安装教程版本较老,而各安装包更新很快,参考价值有限.走了很多弯路才装好,把这个过程记录下来,希望对大家有帮助~ ~ 我的配置:wi ...
- paper 39 :Matlab绘制误差棒图(errorbar函数的使用)
同很多非数学相关专业的朋友一样,我第一次碰到这个图时也是丈二和尚摸不着头脑.只知道这个工字型的图案,中间的点代表的是平均值,上下的两条横线代表的是方差值,除此之外,连这个图叫什么名字都不知道,只好硬着 ...
- String,StringBuffer,StringBuilder三者区别
String:每次改变,String都会重新构造,内存指针都会改变 StringBuffer:主要用在全局变量中 StringBuilder:在线程内完成字符拼接,因为线程是不安全的,所以完成后可以丢 ...