POJ1861 Network(Kruskal)(并查集)
Network
Time Limit: 1000MS
Memory Limit: 30000K | ||||
Total Submissions: 16047 | Accepted: 6362 | Special Judge |
Description
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
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
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)(并查集)的更多相关文章
- poj1861 network(并查集+kruskal最小生成树
题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...
- 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 ...
- Minimum Spanning Tree.prim/kruskal(并查集)
开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...
- POJ1861 Network (Kruskal算法 +并查集)
Network Description Andrew is working as system administrator and is planning to establish a new net ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...
- POJ 2236:Wireless Network(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 36363 Accepted: 150 ...
随机推荐
- 【题解】HNOI2008GT考试
这题好难啊……完全不懂矩阵加速递推的我TAT 这道题目要求我们求出不含不吉利数字的字符串总数,那么我们有dp方程 : dp[i][j](长度为 i 的字符串,最长与不吉利数字前缀相同的后缀长度为 j ...
- Boosting&Bagging
Boosting&Bagging 集成学习方法不是单独的一个机器学习算法,而是通过构建多个机器学习算法来达到一个强学习器.集成学习可以用来进行分类,回归,特征选取和异常点检测等.随机森林算法就 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- java实现极简的LRU算法
import java.util.LinkedHashMap;import java.util.Map; /** * LRU (Least Recently Used) */public class ...
- scala(一种静态语言)
语法: 关键字 val(表示:值) 不可变 ex: val a:Int=1 或者 val a=1(会自动识别类型,无基本类与包装类之分) 输出:a:Int=1 关键字var ex: var a ...
- WebOS系列-了解Wekbit【邓侃】
注:[转载请注明文章来源.保持原样] 出处:http://www.cnblogs.com/jyli/archive/2010/02/02/1660634.html 作者:李嘉昱 这是Kan老大的We ...
- 【bzoj1597- [Usaco2008 Mar]土地购买】斜率优化
[597][Usaco2008 Mar]土地购买 [题目描述] 有N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000 ...
- Kali 1.0 / 2.0 安装中文输入法(谷歌pinyin + 其他)
1.kali默认是没有中午输入法的,需要自己安装一下 2.首先我们先获取root权限 dnt@HackerKali:~$ su密码: 3.安装中文输入法(apt-get 指令不会的同学可以学习一下基础 ...
- kickstart构建Live CD 添加文件问题
在构建自定义ISO的时候,有时候需要从母体机器拷贝文件到Live CD系统.比如拷贝/home/xiaoxiaoleo/hello 程序,在Kickstart配置文件里, post脚本添加--noch ...
- 【MySQL优化】使用show status查看MySQL服务器状态信息
在网站开发过程中,有些时候我们需要了解MySQL的服务器状态信息,譬如当前MySQL启动后的运行时间,当前MySQL的客户端会话连接数,当前MySQL服务器执行的慢查询数,当前MySQL执行了多少SE ...