Network

Time Limit: 1000MS

    Memory Limit: 30000K
Total Submissions: 16047   Accepted: 6362   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
【分析】首先,这一题有问题。第一,输入文件包含多个测试用据,他没说;第二,测试用例的结果错了,应该是
1
3
1 2
1 3
3 4
而且应该是多判的,可以用Kruskal;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
vector<int>q;
struct Edg {
int v,u;
int w;
} edg[M];
bool cmp(Edg g,Edg h) {
return g.w<h.w;
}
int n,m,k,maxn;
int parent[N];
void init() {
for(int i=; i<n; i++)parent[i]=i;
}
void Build() {
int u,v,w;
for(int i=; i<m; i++) {
scanf("%d%d%d",&u,&v,&w);
edg[i].u=u;
edg[i].v=v;
edg[i].w=w;
}
sort(edg,edg+m,cmp);
}
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}//查找并返回节点x所属集合的根节点
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}//将两个不同集合的元素进行合并
void Kruskal() {
int sum=;
int num=;
int u,v;
for(int i=; i<m; i++) {
u=edg[i].u;
v=edg[i].v;
if(Find(u)!=Find(v)) {
sum+=edg[i].w;
maxn=max(maxn,edg[i].w);
q.push_back(i);
num++;
Union(u,v);
}
if(num>=n-) {
printf("%d\n%d\n",maxn,n-); break;
}
}
}
int main() {
while(~scanf("%d%d",&n,&m)) {
while(!q.empty())q.pop_back();
maxn=-;
init();
Build();
Kruskal();
for(int i=; i<q.size(); i++) {
int l=q[i];
printf("%d %d\n",edg[l].u,edg[l].v);
}
}
return ;
}

POJ1861 Network(Kruskal)(并查集)的更多相关文章

  1. poj1861 network(并查集+kruskal最小生成树

    题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...

  2. TOJ 2815 Connect them (kruskal+并查集)

    描述 You have n computers numbered from 1 to n and you want to connect them to make a small local area ...

  3. Minimum Spanning Tree.prim/kruskal(并查集)

    开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...

  4. Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

    题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...

  5. POJ1861 Network (Kruskal算法 +并查集)

    Network Description Andrew is working as system administrator and is planning to establish a new net ...

  6. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  7. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  8. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

  9. POJ 2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 36363   Accepted: 150 ...

随机推荐

  1. 深入探讨Android异步精髓Handler

    探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架 ...

  2. 【BZOJ 4565】 [Haoi2016]字符合并 区间dp+状压

    考试的时候由于总是搞这道题导致爆零~~~~~(神™倒序难度.....) 考试的时候想着想着想用状压,但是觉得不行又想用区间dp,然而正解是状压着搞区间,这充分说明了一件事,状压不是只是一种dp而是一种 ...

  3. 手把手教你通过Eclipse工程配置调用JNI完全攻略

    本文地址:http://www.cnblogs.com/wavky/p/JNI.html 当你找到并鬼使神差地打开这个博文的时候,我敢肯定你已经知道什么是JNI,基本概念就不粘贴了. 百度出来的JNI ...

  4. Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem

    E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...

  5. 调用webservice接口

    这里是cxf服务器,采用myeclipse6.5,把wsdl放到本地的方式. 新建一个包, 把解析到的类放在这个包下面. 生成的代码结构: 调用: public static String callI ...

  6. 转:java 帐号激活与忘记密码 实例

    原文链接:http://endual.iteye.com/blog/1613679 一.帐户激活   在 很多时候,在某些网站注册一个用户之后,网站会给这个用户注册时填写的email地址发送一封帐户激 ...

  7. [bzoj3532][Sdoi2014]Lis——拆点最小割+字典序+退流

    题目大意 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性 ...

  8. PHP中HTTP_X_FORWARDED_FOR、REMOTE_ADDR和HTTP_CLIENT_IP

    1.REMOTE_ADDR:浏览当前页面的用户计算机的ip地址 2.HTTP_X_FORWARDED_FOR: 浏览当前页面的用户计算机的网关 3.HTTP_CLIENT_IP:客户端的ip 在PHP ...

  9. Codeforces 950E Data Center Maintenance 强连通分量

    题目链接 题意 有\(n\)个信息中心,每个信息中心都有自己的维护时间\((0\leq t\lt h)\),在这个时刻里面的信息不能被获得. 每个用户的数据都有两份备份,放在两个相异的信息中心(维护时 ...

  10. Ubuntu破解开机密码

    使用Ubuntu和使用windows系列产品一样,会忘记开机密码.难道我们在忘记开机密码的时候就必须重装系统吗?当然不是了!既然在windows下面我们可以破解开机密码,那么在Ubuntu里面一样可行 ...