Network

Time limit: 1.0 second
Memory limit: 64 MB
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 contains two integer: 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 output
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
1
4
1 2
1 3
2 3
3 4
Problem Author: Andrew Stankevich

题意:

给定几个需要链接的点以及能够利用的边,要求用这些变将所有的点都链接起来,且所用边长的最大值尽量小。

思路:

起初,看题意是最小生成树,但题目所给的样例,并不是最小生成树的结果,纠结半天,然后又看了几遍题目,还是不知所云,最后试着把模版敲上去,submit~~

然后便ac了,

后来问了才知道,这道题确实最小生成树,不过只要所用边的最大值不变,那些权值小的边可以任意加上去,因为题目只是对最大边最小有要求,对边的数量没有要求。

#include <stdio.h>
#include <string.h>
#include <cmath>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,k,s,t,tot,sum=,maxn=;
int head[N],vis[N],dis[N],father[N];
int dfn[N],low[N],stack1[N],num[N],in[N],out[N];
struct man{
int u,v,val,used;
}edg[M];
bool cmp(man f,man g){
return f.val<g.val;
}
int find(int x){
if(father[x]!=x)father[x]=find(father[x]);
return father[x];
}
void Union(int x,int y){
x=find(x);y=find(y);
if(x!=y)father[y]=x;
return;
}
void kruskal(){
for(int i=;i<=m;i++){
int u=edg[i].u,v=edg[i].v;
if(find(u)==find(v))continue;
Union(u,v);
sum++;edg[i].used=;
maxn=edg[i].val;
if(sum==n-)return;
}
}
int main() {
int u,v,val;tot=;met(dfn,);met(vis,);met(head,-);
for(int i=;i<N;i++)father[i]=i;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&val);
edg[i].u=u;edg[i].v=v;edg[i].val=val;edg[i].used=;
}
sort(edg+,edg+m+,cmp);
kruskal();
printf("%d\n%d\n",maxn,sum);
for(int i=;i<=m;i++){
if(edg[i].used){
printf("%d %d\n",edg[i].u,edg[i].v);
}
}
return ;
}

URAL 1160 Network(最小生成树)的更多相关文章

  1. 1160. Network(最小生成树)

    1160 算是模版了 没什么限制 结束输出就行了 #include <iostream> #include<cstdio> #include<cstring> #i ...

  2. [poj2349]Arctic Network(最小生成树+贪心)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17758   Accepted: 5646 D ...

  3. BZOJ 3732: Network 最小生成树 倍增

    3732: Network 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 &l ...

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

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

  5. ZOJ1586——QS Network(最小生成树)

    QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...

  6. TZOJ 2415 Arctic Network(最小生成树第k小边)

    描述 The Department of National Defence (DND) wishes to connect several northern outposts by a wireles ...

  7. ZOJ1586:QS Network (最小生成树)

    QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...

  8. poj2349 Arctic Network - 最小生成树

    2017-08-04 16:19:13 writer:pprp 题意如下: Description The Department of National Defence (DND) wishes to ...

  9. POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~

    Network Time Limit: 1000MS   Memory Limit: 30000K          Special Judge http://poj.org/problem?id=1 ...

随机推荐

  1. 从协议VersionedProtocol开始

    VersionedProtocol协议是Hadoop的最顶层协议接口的抽象:5--3--3共11个协议,嘿嘿 1)HDFS相关 ClientDatanodeProtocol:client与datano ...

  2. windows azure中国 里面建立一个虚拟机,与虚拟机建立通信 里面部署IIS,外网访问

    在windows azure中国 里面建立一个虚拟机,里面部署IIS,外网不能访问么? 外网访问的地址是给的那个DNS地址 ,比如我的是 DNS 名称 urbanairserver.cloudapp. ...

  3. SpringMvc异常

    局部异常:在controller内部写一个处理异常的方法,注解ExceptionHandler(value={自己弄的异常class}) 这样发生value里面的类的异常,就可以执行这个方法,然后往r ...

  4. IOS界面切换

    好吧!表示这几天要实现  phonegap 打开IOS原生界面,因此也查询了一些方案. 有如下几种: 第一种:navigationcontroller //进入下层 [self.navigationC ...

  5. uart与usart

    字面意义:UART:universal asynchronous receiver and transmitter通用异步收发器:USART:universal synchronous asynchr ...

  6. Mysql5.0以下 手工注入

    order by 20 www. .com/product/introduction.php?id=-65 UNION SELECT user(),2 www. .com/product/introd ...

  7. C++中的::operator new, ::operator delete

    一般在使用new  和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数 但是也有办法将这两个过程分开 那就是显式的调用::operat ...

  8. React Native 组件之Image

    Image组件类似于iOS中UIImage控件,该组件可以通过多种方式加载图片资源. 使用方式,加载方式有如下几种: /** * Sample React Native App * https://g ...

  9. jQuery Transit

    http://code.ciaoca.com/jquery/transit/ jQuery Transit 事件监听 https://developer.mozilla.org/en-US/docs/ ...

  10. 浅谈Android应用性能之内存

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/ jaunty [博主导读]在Android开发中,不免会遇到许多OOM现象,一方面可能是由于开 ...