uva10911 Forming Quiz Teams

You have been given the job of forming the quiz teams for the next
`MCA CPCI Quiz Championship’. There are 2 N students interested to
participate and you have to form N teams, each team consisting of two
members. Since the members have to practice together, all the students
want their members house as near as possible. Let x 1 be the distance
between the houses of group 1, x 2 be the distance between the houses
of group 2 and so on. You have to make sure the summation ( x 1
+ x 2
+ x 3
+ :::
+ x n ) is minimized. Input There will be many cases in the input le. Each case starts with an integer N ( N 8). The next 2 N lines will
given the information of the students. Each line starts with the
students name, followed by the x 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.

状压dp,每次枚举集合中最大的元素和谁配对。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double dp[1<<20],xx[20],yy[20];
char s[100];
double dis(int a,int b)
{
    return sqrt((xx[a]-xx[b])*(xx[a]-xx[b])+(yy[a]-yy[b])*(yy[a]-yy[b]));
}
int main()
{
    int i,j,k,m,n,p,q,x,y,z,K=0;
    while (scanf("%d",&n)&&n)
    {
        n*=2;
        for (i=0;i<n;i++)
          scanf("%s%lf%lf",s+1,&xx[i],&yy[i]);
        memset(dp,127,sizeof(dp));
        dp[0]=0;
        for (i=1;i<(1<<n);i++)
        {
            for (p=n-1;;p--)
              if (i&(1<<p)) break;
            for (j=p-1;j>=0;j--)
              if (i&(1<<j))
                dp[i]=min(dp[i],dp[i-(1<<p)-(1<<j)]+dis(p,j));
        }
        printf("Case %d: %.2f\n",++K,dp[(1<<n)-1]);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值