看了别人的思路才知道特殊情况
#include<stdio.h>
#include<string.h>
#include<math.h>
double radius[10];
bool used[10];
double centerpos[10]; //the position of circle's center
double radpos[10]; //radpos[i] = the radius of the circle here
int n,m;
double boxlen;
void dfs(int cur)
{
if(cur == m)
{
double len = 0;
for(int i=0; i < m; ++i)
{
if(len < centerpos[i]+radpos[i])
{
len = centerpos[i]+radpos[i];
}
}
if(len < boxlen)
{
boxlen = len;
}
return;
}
for(int i=0; i < m; ++i)
{
if(!used[i])
{
used[i] = true;
double maxpos = radius[i];
double dist;
for(int j=0; j < cur; ++j)
{
dist = (radius[i]+radpos[j])*(radius[i]+radpos[j]) -\
(radius[i]-radpos[j])*(radius[i]-radpos[j]);
if(maxpos < centerpos[j] + sqrt(dist) )
{
maxpos = centerpos[j] + sqrt(dist);
}
}
radpos[cur] = radius[i];
centerpos[cur] = maxpos;
if(maxpos + radpos[cur] <= boxlen)
dfs(cur+1);
used[i] = false;
}
}
}
int main()
{
//
freopen("input.txt","r",stdin);
scanf("%d", &n);
while(n--)
{
scanf("%d", &m);
for(int i=0; i < m; ++i)
{
scanf("%lf", &radius[i]);
}
boxlen = 100000000;
memset(used, false, sizeof(used));
for(int i=0; i < m; ++i)
{
centerpos[0] = radius[i];
radpos[0] = radius[i];
used[i] = true;
dfs(1);
used[i] = false;
}
printf("%.3lf\n", boxlen);
}
}