现在的位置: 首页 > 综合 > 正文

hdu 3920 Clear All of Them I 状态压缩DP

2013年07月31日 ⁄ 综合 ⁄ 共 2696字 ⁄ 字号 评论关闭
Problem Description
Acmers have been the Earth Protector against the evil enemy for a long time, now it’s your turn to protect our home.
  There are 2 * n enemies in the map. Your task is to clear all of them with your super laser gun at the fixed position (x, y).
  For each laser shot, your laser beam can reflect 1 times (must be 1 times), which means it can kill 2 enemies at one time. And the energy this shot costs is the total length of the laser path.
  For example, if you are at (0, 0), and use one laser shot kills the 2 enemies in the order of (3, 4), (6, 0), then the energy this shot costs is 5.0 + 5.0 = 10. 00.
  Since there are 2 * n enemies, you have to shot n times to clear all of them. For each shot, it is you that select two existed enemies and decide the reflect order.
  Now, telling you your position and the 2n enemies’ position, to save the energy, can you tell me how much energy you need at least to clear all of them?
  Note that:
   > Each enemy can only be attacked once.
   > All the positions will be unique.
   > You must attack 2 different enemies in one shot.
   > You can’t change your position.
 

 

Input
The first line contains a single positive integer T( T <= 100 ), indicates the number of test cases.
For each case:
  There are 2 integers x and y in the first line, which means your position.
  The second line is an integer n(1 <= n <= 10), denote there are 2n enemies.
  Then there following 2n lines, each line have 2 integers denote the position of an enemy.
  
  All the position integers are between -1000 and 1000.
 

 

Output
For each test case: output the case number as shown and then print a decimal v, which is the energy you need at least to clear all of them (round to 2 decimal places).
 

 

Sample Input
2 0 0 1 6 0 3 0 0 0 2 1 0 2 1 -1 0 -2 0
 

 

Sample Output
Case #1: 6.00 Case #2: 4.41

 

 //

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int inf=(1<<28);
struct Node
{
    int x,y;
};
int n;
Node p[30];
double dp[1<<22];
double a[30][30];
double d[30];
double dis(Node h,Node k)
{
    return sqrt(0.0+(h.x-k.x)*(h.x-k.x)+(h.y-k.y)*(h.y-k.y));
}
bool cmp(Node h,Node k)
{
    return dis(p[0],h)<dis(p[0],k);
}
double dfs(int u)
{
    if(dp[u]>1e-6) return dp[u];
    if(u==0) return 0;
    //这里u最右面的第一个1一定选 可以优化  important
    //越靠右,越是靠近源点
    int l=0;
    while(!(u&(1<<l))) l++;//最右面的1
    double rmin=inf;
    for(int i=l+1;i<n;i++)
    {
        if(u&(1<<i))//是1
        {
            int tmp=u-(1<<l)-(1<<i);
            double cnt=dfs(tmp)+d[l+1]+a[l+1][i+1];
            if(cnt<rmin) rmin=cnt;
        }
    }
    dp[u]=rmin;
    return rmin;
}
int main()
{
    int pl=1,ci;scanf("%d",&ci);
    while(ci--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&p[0].x,&p[0].y);
        scanf("%d",&n);n<<=1;
        for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
        sort(p+1,p+1+n,cmp);
        for(int i=1;i<=n;i++)
        {
            d[i]=dis(p[0],p[i]);
            for(int j=1;j<=n;j++)
            {
                a[i][j]=dis(p[i],p[j]);
            }
        }
        double ans=dfs((1<<n)-1);
        printf("Case #%d: %.2lf\n",pl++,ans);
    }
    return 0;
}

抱歉!评论已关闭.