POJ 1861 ——Network——————【最小瓶颈生成树】
2015-10-30 21:39
495 查看
Network
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
Sample Output
Source
Northeastern Europe 2001, Northern Subregion
题目大意:给你n个顶点m条无向边。让你求一棵生成树,使得最大边权尽量小。输出最长边,边的条数,哪些边。这题是特判,所以跟样例输出可能不太一样。
解题思路:即求最小瓶颈生成树。当用kruskal算法求解的时候。图第一次连通的时候,最后加入的那条边,即为所求。
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 15268 | Accepted: 5987 | Special Judge |
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
Source
Northeastern Europe 2001, Northern Subregion
题目大意:给你n个顶点m条无向边。让你求一棵生成树,使得最大边权尽量小。输出最长边,边的条数,哪些边。这题是特判,所以跟样例输出可能不太一样。
解题思路:即求最小瓶颈生成树。当用kruskal算法求解的时候。图第一次连通的时候,最后加入的那条边,即为所求。
#include<stdio.h> #include<algorithm> #include<string.h> #include<iostream> using namespace std; const int maxn = 1010; const int maxe = 15010; struct Edge{ int from,to,dist,idx; Edge(){} Edge(int _from,int _to,int _dist,int _idx):from(_from),to(_to),dist(_dist),idx(_idx){} }edges[maxe]; struct Set{ int pa,rela; }sets[maxn]; int ans[maxn]; bool cmp(Edge a,Edge b){ return a.dist < b.dist; } void init(int n){ for(int i = 0; i <= n; i++){ sets[i].pa = i; } } int Find(int x){ if(x == sets[x].pa){ return x; } int tmp = sets[x].pa; sets[x].pa = Find(tmp); return sets[x].pa; } int main(){ int n, m; while(scanf("%d%d",&n,&m)!=EOF){ init(n); int a,b,c; for(int i = 0; i < m; i++){ scanf("%d%d%d",&a,&b,&c); edges[i] = Edge(a,b,c,i); } sort(edges,edges+m,cmp); int cnt = 0; for(int i = 0; i < m; i++){ Edge & e = edges[i]; int rootx, rooty; rootx = Find(e.from); rooty = Find(e.to); if(rootx == rooty){ continue; } sets[rooty].pa = rootx; ans[cnt++] = i; } printf("%d\n",edges[ans[cnt-1]].dist); printf("%d\n",cnt); for(int i = 0; i < cnt; i++){ printf("%d %d\n",edges[ans[i]].from,edges[ans[i]].to); } } return 0; }
相关文章推荐
- lintcode 中等题:continuous subarray sum 连续子数组之和
- uvaoj 1169 Robotruck
- Hadoop集群配置中如何写入和读取数据?
- low_bound,upper_bound函数的简单介绍
- erlang和go之间桥接库相关
- IIS 下配置对php的支持
- CCPC-CDOJ-1221-G题
- Bootstrap:简洁、直观、强悍的前端开发框架
- 浅谈求职之路之网易面经
- 编程语言中常见的判断、选择以及循环语句的使用场合
- Ubuntu14.01 root登录面板
- Single Number
- 从抽象类到模板方法模式
- OHCI(一) - OHCI spec
- 修改原有的方法名称(字节码增强)
- 私有地址范围
- 2015年大二上-数据结构-链表(2)- 建设“单链表”算法库
- Codeforces Round #327 (Div. 2) (591A,591B,590A(找规律),590B(二分))
- 瀑布流 集合视图
- 一个对象实例化的过程