uva 10635 - Prince and Princess

最喜欢dp了。。。

状态压缩?裸的

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
double dp[(1<<20)+1];
int cas=1,n;
double x[30],y[30],d[30][30];
char s[100];
double dist(int i,int j)
{
double a=x[i]-x[j],b=y[i]-y[j];
return sqrt(a*a+b*b);
}
double DP(int st)
{
if(dp[st]>=0)
return dp[st];
dp[st]=1000000000;
for(int i=0;(1<<i)<=st;i++)
for(int j=0;(1<<j)<=st;j++)
if(i!=j&&((1<<i)&st)&&((1<<j)&st))
dp[st]=min(dp[st],d[i][j]+DP(st-(1<<i)-(1<<j)));
return dp[st];
}
int main()
{
while(scanf("%d",&n),n)
{
memset(dp,-1,sizeof(dp));

dp[0]=0;
for(int i=0;i<n*2;i++)
{
scanf("%s%lf%lf",s,&x[i],&y[i]);
for(int j=i-1;j>=0;j--)
d[i][j]=d[j][i]=dist(i,j);
}
printf("Case %d: %.2lf\n",cas++,DP((1<<(n*2))-1));
}
return 0;
}

 

你可能感兴趣的:(uva)