uva10911 - Forming Quiz Teams 集合DP

  第一次做配对问题,集合DP原来是这个意思啊。。和状态压缩DP差不多。

  给你一些点,要求两两配对,问这些配对点距离和的最小值。

  d(i,S)表示把前i个点中,位于集合S中的元素两两配对的最小距离,所以有d(i,S) = min{|PiPj|+ d(i-1,S-{i}-{j}) | j属于S}。

  1代表这个点还在,0代表不在了,比如有6个点d[111111]就是要求的答案,它就等于p[5][0]+d[011110],p[5][1]+d[011101],p[5][2]+d[011011],p[5][3]+d[010111],p[5][4]+d[001111]中的最小值。这时只要求出子问题就行了,可以递归记忆化搜索,也可以从000001循环到111111,把所有状态求一遍,不过显然是前面的方法快,因为不用求奇数的情况。


You have been given the job of forming the quiz teams for the next ‘MCA CPCI Quiz Championship’. There are2*Nstudents interested to participate and you have to formN 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 integerN (N ≤ 8).The next2*N lines will given the information of the students. Each line starts with the student’s name, followed by thex coordinate and then they coordinate. Bothx, 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


算出全部状态 0.262s


#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int a[20],b[20],N,test=0;
double d[70000];
double dist(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double DP(){
    int s,i,j;
    for(s=1;s<(1<<N);s++){
        for(i=N-1;i>=0;i--) if(s&(1<<i)) break;
        for(j=0;j<i;j++) if(s&(1<<j)){
            d[s]=min(d[s],dist(a[i],b[i],a[j],b[j])+d[s^(1<<i)^(1<<j)]);
        }
    }
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N),N){
        int i;
        char s[25];
        N*=2;
        for(i=0;i<N;i++) scanf("%s%d%d",s,&a[i],&b[i]);
        d[0]=0;
        for(i=1;i<=(1<<N)-1;i++) d[i]=INF;
        DP();
        printf("Case %d: %.2lf\n",++test,d[(1<<N)-1]);
    }
    return 0;
}

记忆化搜索 0.032s

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int a[20],b[20],N,test=0;
double d[70000];
double dist(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double DP(int s){
    if(d[s]!=-1) return d[s];
    double &ans=d[s];
    ans=INF;
    int i,j;
    for(i=N-1;i>=0;i--) if(s&(1<<i)) break;
    for(j=0;j<i;j++) if(s&(1<<j)){
        ans=min(ans,dist(a[i],b[i],a[j],b[j])+DP(s^(1<<i)^(1<<j)));
    }
    return ans;
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N),N){
        int i;
        char s[25];
        N*=2;
        for(i=0;i<N;i++) scanf("%s%d%d",s,&a[i],&b[i]);
        d[0]=0;
        for(i=1;i<=(1<<N)-1;i++) d[i]=-1;
        DP((1<<N)-1);
        printf("Case %d: %.2lf\n",++test,d[(1<<N)-1]);
    }
    return 0;
}



你可能感兴趣的:(uva10911 - Forming Quiz Teams 集合DP)