状压dp UVa 10911 - Forming Quiz Teams

You have been given the job of forming the quiz teams for the next ‘MCA CPCI Quiz Championship’. There are2*N students interested to participate and you have to form teams, each team consisting of two members. Since the members have to practice together, all the students want their member’s house as near as possible. Let x1 be the distance between the houses of group 1, x2 be the distance between the houses of group 2 and so on. You have to make sure the summation (x1 + x2 + x3 + …. + xn) is minimized.

Input

There will be many cases in the input file. Each case starts with an integer N (N ≤ 8). The next 2*Nlines will given the information of the students. Each line starts with the student’s name, followed by thex coordinate and then the y coordinate. Both x, y are integers in the range 0 to 1000. Students name will consist of lowercase letters only and the length will be at most 20.

Input is terminated by a case where N is equal to 0.

Output

For each case, output the case number followed by the summation of the distances, rounded to 2 decimal places. Follow the sample for exact format.

Sample Input

Output for Sample Input

5
sohel 10 10
mahmud 20 10
sanny 5 5
prince 1 1
per 120 3
mf 6 6
kugel 50 60
joey 3 24
limon 6 9
manzoor 0 0
1
derek 9 9
jimmy 10 10

0

Case 1: 118.40
Case 2: 1.41

 

#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include<cstdio>
#include <cstring>
#include <queue>
#include<bitset>
typedef long long ll;

using namespace std;
const double inf=1e17;
double x[100],y[100];
double dp[1<<17];
double g[100][100];
int n,m;
int G[1001][1001],f[800000],vis[5555],t;
int p,q;


struct node
{
    int x,y;
}no[100];


double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int main()
{
    int flag=1;
    while(cin>>n&&n)
    {
        n*=2;
        for(int i=0;i<n;i++)
        {
            char s[100];
            scanf("%s %d%d",s,&no[i].x,&no[i].y);
        }
        for(int i=0;i<n;i++)
      {
            for(int j=0;j<n;j++)
        {
            g[i][j]=dis(no[i],no[j]);
        }
      }
       // cout<<g[0][1]<<endl;
        for(int i=0;i<(1<<n);i++)//枚举状态
            dp[i]=inf;
        dp[0]=0;
        for(int i=1;i<(1<<n);i++)
        {
            int k,j;
            for( k=0;k<n;k++)
            {
                if(i&(1<<k))//先找出最右边的1的位置
                    break;
            }
            for(j=k+1;j<n;j++)
            {
                if(i&(1<<j))//枚举其它1的位置
                    {
                        dp[i]=min(dp[i],dp[i^(1<<k)^(1<<j)]+g[k][j]);//与之前包含这两个位置的状态比较
                    }
            }
            //cout<<i<<' '<<dp[i]<<endl;
        }
        //cout<<(1<<n)-1<<endl;
        printf("Case %d: %.2lf\n",flag++,dp[(1<<n)-1]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值