forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10012.cpp
93 lines (86 loc) · 1.47 KB
/
10012.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
using namespace std;
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define ABS(X) ((X) < 0 ? -(X) : (X))
#define MAX_CIRCLES 8
double R[MAX_CIRCLES], C[MAX_CIRCLES];
double distance(const int k, const int l)
{
// hypot(a, b)?
return sqrt(pow(R[k] + R[l], 2) - pow(ABS(R[k] - R[l]), 2));
}
double width(double *R, const int circles)
{
int i, j;
double max;
if (circles == 1)
{
return 2 * R[0];
}
C[0] = R[0];
for (i = 1; i < circles; i++)
{
for (j = max = 0; j < i; j++)
{
max = MAX(max, C[j] + distance(i, j));
}
C[i] = MAX(R[i], max);
}
for (i = max = 0; i < circles; i++)
{
max = MAX(max, C[i] + R[i]);
}
return max;
}
double minWidth(double *R, const int start, const int circles)
{
if (start == circles - 1)
{
return width(R, circles);
}
else
{
int i;
double tmp, min = .0, w;
for (i = start; i < circles; i++)
{
tmp = R[i];
R[i] = R[start];
R[start] = tmp;
w = minWidth(R, start + 1, circles);
if (w < min || min == .0)
{
min = w;
}
R[start] = R[i];
R[i] = tmp;
}
return min;
}
}
int main()
{
int i, j, inputs, circles;
double radius, width;
if (scanf("%d", &inputs) != 1)
{
return EXIT_SUCCESS;
}
for (i = 0; i < inputs; i++)
{
if (scanf("%d", &circles) != 1)
{
return EXIT_SUCCESS;
}
for (j = 0; j < circles; j++)
{
if (scanf("%lf", &radius) != 1)
{
return EXIT_SUCCESS;
}
R[j] = radius;
}
printf("%.3f\n", minWidth(R, 0, circles));
}
return 0;
}