Description(Special Judge)
Andrew is working as systemadministrator and is planning to establish a new network in his company. Therewill be N hubs in the company, they can be connected to each other usingcables. 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 someintermediate hubs).
Since cables of different types are available and shorter ones are cheaper, itis necessary to make such a plan of hub connection, that the maximum length ofa single cable is minimal. There is another problem — not each hub can beconnected to any other one because of compatibility problems and buildinggeometry limitations. Of course, Andrew will provide you all necessaryinformation about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all aboveconditions are satisfied.
Input
The first line of the inputcontains 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 containinformation about possible connections - the numbers of two hubs, which can beconnected and the cable length required to connect them. Length is a positiveinteger number that does not exceed 106. There will be no more thanone way to connect two hubs. A hub cannot be connected to itself. There willalways be at least one way to connect all hubs.
Output
Output first the maximumlength of a single cable in your hub connection plan (the value you shouldminimize). Then output your plan: first output P - the number of cables used,then output P pairs of integer numbers - numbers of hubs connected by thecorresponding 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
3
1 2
1 3
2 4
首先要说,POJ上的给出的输出是错的。
题目简介:给N个点,M条路。每条路由起点、终点以及距离组成。使用最少的路使全部点连接起来,输出其中用到的路中最长的一条路和使用的路的数目。以及用到每条路的起点和终点。
方法:最小生成树(Kruskal)。要输出回经过的点,只需要将用到的路标记一下,最后输入被标记的路的起、终点即可。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int x, y;
int dis;
}num[15010];
int flag[15010], p[1010], m, n, x, y;
int cmp(const void *a, const void *b)
{
struct node *c, *d;
c = (struct node *)a;
d = (struct node *)b;
if(c->dis!=d->dis)
return c->dis - d->dis;
else if(c->x!=d->x)
return c->x - d->x;
return c->y - d->y;
};
int root(int x)
{
if(p[x]==-1)
{
return x;
}
return p[x] = root(p[x]);
};
void Kruskal()
{
int max = 0,count = 0;
memset(p,-1,sizeof(p));
qsort(num+1,n,sizeof(num[0]),cmp);
for(int i=1;i<=n;i++)
{
x = root(num[i].x);
y = root(num[i].y);
if(x!=y)
{
flag[i] = 1;
if(max<num[i].dis)
{
max = num[i].dis;
}
count++;
p[x] = y;
}
}
printf("%d\n%d\n",max, count);
};
int main()
{
scanf("%d%d",&m,&n);
for(int i = 1;i<=n;i++)
{
scanf("%d%d%d",&num[i].x, &num[i].y, &num[i].dis);
}
memset(flag,0,sizeof(flag));
Kruskal();
for(int i = 1;i<=n;i++)
{
if(flag[i]==1)
{
printf("%d %d\n",num[i].x,num[i].y);
}
}
system("pause");
return 0;
}