ZOJ-Network(最小生成树)

Network

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

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1542

题意描述:

给你n个点m条边,求把所有的点连起来的最短路中边的最大值,并输出建边的顺序

解题思路:

首先把所有的边存到结构体里,然后用克鲁斯卡尔算法,在连边的时候记录要输出边的下标存到数组里,然后输出最大边和各个边的顺序。

程序代码:

#include<stdio.h>
#include<algorithm>
using namespace std;

struct data{
	int u;
	int v;
	int w;
}e[15010];

int n,m;
int f[1010],a[1010];

int cmp(data x,data y);
int getf(int u);
int merge(int u,int v);
void kruscal();

int main()
{
	int i,sum;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=1;i<=m;i++)
			scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		sort(e+1,e+m+1,cmp);
		kruscal();
		for(i=0;i<n-1;i++)
			printf("%d %d\n",e[a[i]].u,e[a[i]].v);
	}
	return 0;
}

int cmp(data x,data y)
{
	return x.w<y.w;
}

int getf(int u)
{
	if(f[u]==u)
		return u;
	f[u]=getf(f[u]);
	return f[u];
}

int merge(int u,int v)
{
	u=getf(u);
	v=getf(v);
	if(u!=v)
	{
		f[v]=u;
		return 1;
	}
	return 0;
}

void kruscal()
{
	int i,count=0,maxn=-1;
	for(i=1;i<=n;i++)
		f[i]=i;
	for(i=1;i<=m;i++)
	{
		if(merge(e[i].u,e[i].v)==1)
		{
			a[count++]=i;
			maxn=max(maxn,e[i].w);
		}
		if(count==n-1)
			break;
	}
	printf("%d\n%d\n",maxn,n-1);
}

猜你喜欢

转载自blog.csdn.net/HeZhiYing_/article/details/83994385