uva 10911 Forming Quiz Teams

状态压缩集合上的dp

uva 10911 Forming Quiz Teams

#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 10
#define INF 1<<30
double dp[1<<(N*2)];
int x[N*2],y[N*2],n;
double min(double a,double b)
{
    return a>b?b:a;
}
double dis(int a,int b)
{
    double h;
    h=(x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);
    return sqrt(h);
}
int main()
{
    int i,j,k,l,st;
	k=1;
    while(scanf("%d",&n)&&n)
    {
        char str[30];
        for(i=0;i<n*2;i++)
        {
            scanf("%s",str);
            scanf("%d %d",&x[i],&y[i]);
        }
        for(i=0;i<(1<<(n*2));i++)
            dp[i]=INF;
		dp[0]=0;
        for(i=1;i<n*2;i++)//每个i与i之后的状态中第j个人分组
        {
            for(st=0;st<(1<<i);st++)
            {
                for(j=0;j<i;j++)
                {
                    if(!(st&(1<<j)))//j在st状态中
                        continue;
                    if(dp[st^(1<<j)]!=INF)//st中除了j是双数,可以
                    {
                        dp[st|(1<<i)]=min(dp[st|(1<<i)],dis(i,j)+dp[st^(1<<j)]);
                    }
                }
            }
        }
        printf("Case %d: %.2f\n",k++,dp[(1<<(n*2))-1]);
    }
    return 0;
}


你可能感兴趣的:(uva 10911 Forming Quiz Teams)