Network
Time Limit: | Memory Limit: | |||
Total Submissions: | Accepted: | 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.
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 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.
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
Source
Northeastern Europe 2001, Northern Subregion
题很简单,但是由于Sample上的数据太变态,结果一直不敢交,
这个题目的答案可以有环,也可以无环(只要满足最长边最短)。只不过无环的话简单的多了,用Kruscal就好了(因为用Kruscal求最小生成树的时候恰好具有最大边最短的性质,但prim只能求最小生成树,不能保证这个性质)
代码:
C语言: 临时自用代码
#include<stdio.h>
#include<stdlib.h>
struct
point
{
int
x
,
y
,
lenth;
} map [ 15008 ];
int
flag
[
1008
];
int
cmp(
const
void
*
a
,
const
void
*b)
{
struct
point
*
c
,
*
d;
c
=(
struct
point
*)
a;
d
=(
struct
point
*)b;
return
c
->
lenth
-
d
->
lenth;
}
int
father(
int
x)
{
if(
x
==
flag
[
x
])
return
x;
flag
[
x
]
=
father(
flag
[
x
]);
return
flag
[
x
];
}
int
main()
{
int
m
,n
,
i
,
k
,
fa
,
fb
,
max;
int
a
[
15008
],b
[
15008
];
while(
scanf(
"%d%d"
,
&n
,
&
m)
!=
EOF)
{
for(
i
=
0;
i
<
m;
i
++)
scanf(
"%d%d%d"
,
&
map
[
i
].
x
,
&
map
[
i
].
y
,
&
map
[
i
].
lenth);
for(
i
=
0;
i
<=n;
i
++)
flag
[
i
]
=
i;
qsort(
map
,
m
,
sizeof(
map
[
0
]),
cmp);
k
=
0;
for(
i
=
0;
i
<
m;
i
++)
{
fa
=
father(
map
[
i
].
x);
fb
=
father(
map
[
i
].
y);
if(
fa
!=
fb
){
if(
fa
>
fb)
flag
[
fa
]
=
fb;
else
flag
[
fb
]
=
fa;
a
[
k
]
=
map
[
i
].
x;
b
[
k
]
=
map
[
i
].
y;
k
++;
if(
max
<
map
[
i
].
lenth)
max
=
map
[
i
].
lenth;
}
}
printf(
"%d
\n
"
,
max);
printf(
"%d
\n
"
,
k);
for(
i
=
0;
i
<
k;
i
++)
printf(
"%d %d
\n
"
,
a
[
i
],b
[
i
]);
}
return
0;
}
#include<stdlib.h>
struct
} map [ 15008 ];
int
int
{
}
int
{
}
int
{
}