首页 > Web开发 > 详细

poj 1861 Network

时间:2014-07-21 23:27:22      阅读:398      评论:0      收藏:0      [点我收藏+]
Network
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13260   Accepted: 5119   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


题意:给出节点个数n和边数m,下面m行给出边(x,y)以及权值p。输出第一行为最小生成树中的最大边权值,第二行为一个可行生成树方案的边数k,下面k行为可行生成树的k条边。

题目是Special Judge

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define M 1500000
int f[M];
struct node
{
	int x,y,l;
}p[M],v[M];

bool cmp(node a,node b)
{
	return (a.l<b.l);
}

void Init (int n)
{
	for(int i=1;i<=n;i++)
		f[i]=i;
}

int find(int x)
{
	if(x!=f[x])
		return f[x]=find(f[x]);
	return f[x];
}

void Union(int x,int y)
{
	int fx,fy;
	fx=find(x);
	fy=find(y);
	if(fx!=fy)
		f[fy]=fx;
}

int main ()
{
	int n,m;
	int i;
	cin>>n>>m;
	Init(n);
	for(i=0;i<m;i++)
		cin>>p[i].x>>p[i].y>>p[i].l;
	sort(p,p+m,cmp);

	int k=0;
	int max=0;
	
	for(i=0;i<m;i++)    // 寻找最小生成树
	{
		int k1=find(p[i].x);
		int k2=find(p[i].y);
		if(k1!=k2)
		{
			k++;     // 边的条数
			Union(k1,k2);
			v[k]=p[i];  // 记录可行生成树的边
			if(p[i].l>max) max=p[i].l;  // 记录最大的边权值
		}
	}
	
	cout<<max<<endl<<k<<endl;
	for(i=1;i<=k;i++)
		cout<<v[i].x<<' '<<v[i].y<<endl;
	return 0;
}



poj 1861 Network,布布扣,bubuko.com

poj 1861 Network

原文:http://blog.csdn.net/fyxz1314/article/details/38020709

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!