zoj 1542 Network(最小生成树))
2014-09-10 23:43
453 查看
Network
Time Limit: 2 Seconds Memory Limit: 65536 KB 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 file 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
10^6. 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.
Process to the end of file.
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
有N个集线器,集线器之间需要网线连接,设计一个网络,连接所有的集线器,并且该方案中最长的单根网线在所有方案中是最小的。
首先输出方案中最长的网线的长度再输出所使用的网线数目,再输出所连接集线器的编号。
说白了就是最小生成树。
Time Limit: 2 Seconds Memory Limit: 65536 KB 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 file 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
10^6. 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.
Process to the end of file.
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
有N个集线器,集线器之间需要网线连接,设计一个网络,连接所有的集线器,并且该方案中最长的单根网线在所有方案中是最小的。
首先输出方案中最长的网线的长度再输出所使用的网线数目,再输出所连接集线器的编号。
说白了就是最小生成树。
#include <iostream> #include <algorithm> using namespace std; struct node { int u; int v; int w; }; int cmp(node a,node b) { return a.w<b.w; } int father[1010]; int Find(int x) { while (x!=father[x]) { x=father[x]; } return x; } void Union(int u,int v) { u=Find(u); v=Find(v); if(u!=v) { father[u]=v; } } int main() { int n,m; node edges[15001]; while (cin>>n>>m) { for (int i=1; i<=n; i++) { father[i]=i; } for (int i=0; i<m; i++) { cin>>edges[i].u>>edges[i].v>>edges[i].w; } sort(edges, edges+m, cmp); int max=0; //最长网线 node num[15001]; //输出的集线器的编号 int ans=0; //网线数量 for (int i=0; i<m; i++) { if (Find(edges[i].u)!=Find(edges[i].v)) { Union(edges[i].u, edges[i].v); if(max<edges[i].w) { max=edges[i].w; } num[ans].u=edges[i].u; num[ans].v=edges[i].v; ans++; } if(ans>=n-1) { break; } } cout<<max<<endl; cout<<ans<<endl; for (int i=0; i<ans; i++) { cout<<num[i].u<<" "<<num[i].v<<endl; } } return 0; }
相关文章推荐
- POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)
- 【最小生成树】 ZOJ 1542 Network
- 【最小生成树】 ZOJ 1542 Network
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- network 最小生成树 Kruskal 算法 poj 1861 zoj 1542
- poj1861&&zoj1542 Network ——最小生成树入门题_Kruscal算法
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- POJ 1861 && ZOJ 1542--Network 【最小生成树 && kruscal && 水题】
- POJ 1861||ZOJ1542 kruskal求最小生成树(POJ秒杀啊)
- ZOJ 1586 QS Network Kruskal求最小生成树
- ZOJ 1586 : QS Network - prim算法解决最小生成树问题
- POJ 2349&&ZOJ 1914 Arctic Network(kuangbin带你飞 专题六:最小生成树)
- ZOJ QS Network(最小生成树)
- POJ 1861||ZOJ1542 kruskal求最小生成树(POJ秒杀啊)
- zoj1586 QS Network ——最小生成树入门题_Prim算法
- ZOJ 3456 Traveler Nobita 最小生成树
- 最小生成树 zoj-1586 QS Network
- URAL - 1160 Network(最小生成树)
- zoj1203 Swordfish ——最小生成树入门题_Kruscal算法
- zoj 1258 最小生成树