[cpp]
描述:集合題目,劉汝佳書上有這道題目,不過他的代碼有點漏洞,需要改一點才能過
#include <cstdio>
#include <cstring>
#include <cmath>
int n,t=0;
char str[1010];
double v[25][2],d[(1<<20)];
double min(double x,double y)
{
return x>y?y:x;
}
double solve(int x,int y)
{
double a=v[x][0]-v[y][0],b=v[x][1]-v[y][1];
return sqrt(a*a+b*b);
}
int main()
{
//freopen("a.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
if(!n) break;
n=n*2;
for(int i=0; i<n; i++) scanf("%s %lf %lf",str,&v[i][0],&v[i][1]);
d[0]=0;
for(int s=1; s<(1<<n); ++s)
{
d[s]=0x7fffffff;
int i,j;
for(i=0; i<n; ++i) if(s&(1<<i)) break;
for(j=i+1; j<n; ++j)
if(s&(1<<j)) d[s]=min(d[s],solve(i,j)+d[s^(1<<i)^(1<<j)]);
}
printf("Case %d: %.2lf\n",++t,d[(1<<n)-1]);
}
return 0;
}*