1. 程式人生 > >ZOJ-Network(最小生成樹)

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);
}