您的位置:首页 > 产品设计 > UI/UE

UVA 10911 Forming Quiz Teams(状压DP)

2015-05-14 20:11 357 查看
Description





4thIIUCInter-University Programming
Contest, 2005
G
Forming Quiz Teams
Input: standard input

Output: standard output

Problemsetter: Sohel Hafiz
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 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*N lines will given the information of the students. Each
line starts with the student’s 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
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<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
double dp[(1<<16)+10];
int x[20],y[20];
int n;
double Cal(int x1,int x2)
{
    return sqrt(1.0*(x[x1]-x[x2])*(x[x1]-x[x2])+1.0*(y[x1]-y[x2])*(y[x1]-y[x2]));
}
int main()
{
    char str[20];
    int cas=1;
    while(~scanf("%d",&n)&&n)
    {
        n*=2;
        REP(i,n)
          scanf("%s%d%d",str,&x[i],&y[i]);
        int st=1<<n;
        for(int i=0;i<st;i++) dp[i]=INF;
        dp[0]=0;
        for(int s=0;s<st;s++)//1e5*100;
        {
            for(int k1=0;k1<n;k1++)
            {
                if(s&(1<<k1))
                {
                    for(int k2=k1+1;k2<n;k2++)
                    {
                        if(s&(1<<k2))
                        {
                            int pr=s^(1<<k1)^(1<<k2);
                            dp[s]=min(dp[s],dp[pr]+Cal(k1,k2));
                        }
                    }
                }
            }
        }
        printf("Case %d: %.2f\n",cas++,dp[st-1]);
    }
    return 0;
}

/*

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: