UVA_10911_FormingQuizTeams

18 篇文章 0 订阅
2 篇文章 0 订阅

10911 - Forming Quiz Teams

Time limit: 3.000 seconds

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 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 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.
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
Sample Output
Case 1: 118.40
Case 2: 1.41

这个问题可以使用状态压缩进一步提升程序效率

dfs直接也可以过

毕竟一次选两个最多16个,而且每个都要选并且没有顺序

16!!(隔一项的阶乘)还是可以接受的

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;

const int M=20;
double px[M];
double py[M];
double dis[M][M];   //点间距离
int isu[M];

double tot;

void dfs(int cn,int now,double t,int n)  //cn目前已经用了的数,now目前要填的位置,t目前的和,n总数
{
    if(cn==n)
    {
        tot=min(tot,t);
        return;
    }
    isu[now]=1;
    for(int i=now+1;i<n;i++)            //这里注意用过没用过的关系
    {
        if(isu[i])
            continue;
        isu[i]=1;
        int p=now+1;
        while(isu[p])                  //寻找下个能填的位置
           p++;
        dfs(cn+2,p,t+dis[now][i],n);
        isu[i]=0;
    }
    isu[now]=0;
}

int main()
{
    int n;
    //freopen("1.out","w",stdout);
    int ca=1;
    while(1)
    {
        scanf("%d",&n);
        if(!n)
            break;
        tot=1e8;
        memset(isu,0,sizeof(isu));
        n*=2;
        for(int i=0;i<n;i++)
            scanf("%*s%lf%lf",&px[i],&py[i]);
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                dis[i][j]=sqrt((px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]));
        dfs(0,0,0,n);
        printf("Case %d: %.2lf\n",ca++,tot); //注意输出格式
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值