Freckles
Time Limit: | Memory Limit: | |
Total Submissions: | Accepted: |
Description
In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.
Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
Input
The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
Source
还是差不多,最小生成树,自己求出距离,快排+并查集
代码:
C语言: 临时自用代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct
point
{
int
x
,
y;
double
lenth;
} map [ 10008 ];
int
flag
[
108
];
double
x
[
108
],
y
[
108
];
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
?
1
:-
1;;
}
int
father(
int
x)
{
if(
x
==
flag
[
x
])
return
x;
flag
[
x
]
=
father(
flag
[
x
]);
return
flag
[
x
];
}
double
Distance(
int
i
,
int
j)
{
return
sqrt((
x
[
i
]
-
x
[
j
])
*(
x
[
i
]
-
x
[
j
])
+(
y
[
i
]
-
y
[
j
])
*(
y
[
i
]
-
y
[
j
]));
}
int
main()
{
int
n
,
i
,
j
,
k
,
fa
,
fb;
double
max;
while(
scanf(
"%d"
,
&n)
!=
EOF)
{
k
=
0;
for(
i
=
1;
i
<=n;
i
++)
{
scanf(
"%lf %lf"
,
&
x
[
i
],
&
y
[
i
]);
flag
[
i
]
=
i;
}
for(
i
=
1;
i
<=n;
i
++)
for(
j
=
1;
j
<=n;
j
++)
{
map
[
k
++
].
x
=
i;
map
[
k
-
1
].
y
=
j;
map
[
k
-
1
].
lenth
=
Distance(
i
,
j);
}
qsort(
map
,
k
,
sizeof(
map
[
0
]),
cmp);
max
=
0;
for(
i
=
0;
i
<
k;
i
++)
{
fa
=
father(
map
[
i
].
x);
fb
=
father(
map
[
i
].
y);
if(
fa
!=
fb
){
if(
fa
>
fb)
flag
[
fa
]
=
fb;
else
flag
[
fb
]
=
fa;
max
+=
map
[
i
].
lenth;
}
}
printf(
"%.2lf
\n
"
,
max);
}
return
0;
}
#include<stdlib.h>
#include<math.h>
struct
} map [ 10008 ];
int
double
int
{
}
int
{
}
double
{
}
int
{
}