题意:给你2*n个点,问它们两两组合,使每对的距离加起来最小。
状态压缩。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define eps 1e-8
#define INF 1000000000000
const int N=70000;
int n;
double map[N];
bool vis[N];
double dp(int);
struct node
{
int x,y;
}man[20];
int main()
{
int t_cnt=0;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
n=n*2;
char temp[100];
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
scanf("%s%d%d",temp,&man[i].x,&man[i].y);
}
double res=dp((1<<n)-1);
printf("Case %d: %.2lf\n",++t_cnt,res);
}
return 0;
}
double dp(int x)
{
bool &flag=vis[x];
double &res=map[x];
if(flag) return res;
else if(x==0){flag=1;res=0;return res;}
else
{
res=INF;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(!(x&(1<<i)&&(x&(1<<j)))) continue;
double temp=sqrt((man[i].x-man[j].x)*(man[i].x-man[j].x)+(man[i].y-man[j].y)*(man[i].y-man[j].y))+eps;
res=min(res,dp(x^(1<<i)^(1<<j))+temp);
}
}
flag=1;
return res;
}
}