Created
October 26, 2011 14:36
-
-
Save vaclavbohac/1316536 to your computer and use it in GitHub Desktop.
Solution to the 10012 UVa Online Judge problem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#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) | |
{ | |
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; | |
} | |
/** | |
* University of Exeter permute algorithm. | |
*/ | |
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(void) | |
{ | |
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment