HDU 3920 Clear All of Them I 状压DP

Clear All of Them I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)
Total Submission(s): 1681    Accepted Submission(s): 544


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
 

题意:有n发炮弹,没发打出去能选择性的连续打死两个敌人,求打死2*n个敌人的最小花费。花费为打死敌人所走的路线长度。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <string>
#include <cmath>
#define INF 0x3f3f3f3f

using namespace std;
    int n;
double dp[(2<<22)+10];
double x[32],y[32];
double mp[50][50];
int vis[(2<<22)+10];
int target;

double fun(double a,double b,double c,double d)
{
    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}

double dfs(int cur)
{
    if(vis[cur]) return dp[cur];

    if(cur==target) return dp[cur]=0;

    vis[cur]=1;
    for(int i=1;i<=2*n;i++)
    {
        if(cur&(1<<i)) continue;
        int f=cur+(1<<i);

        double ans=INF;

        for(int j=i+1;j<=2*n;j++)
        {
            if(f&(1<<j)) continue;
            int ff=f+(1<<j);

            double tmp=dfs(ff)+mp[i][j];
            tmp+=min(mp[0][i],mp[0][j]);
            ans=min(ans,tmp);
        }
        dp[cur]=ans;//保证每次选取i j两点之后,保存的都是最小值
        break;//注意这里一定要break;
    }
    return dp[cur];
}

int main()
{
int t;
int ca=1;
    double sx,sy;
        scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf",&sx,&sy);

        scanf("%d",&n);
        target=(1<<((2*n)+1))-1;

        x[0]=sx;y[0]=sy;

        memset(mp,0,sizeof mp);
        memset(vis,0,sizeof vis);

        for(int i=1;i<=2*n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
            for(int j=0;j<i;j++)
            {
                mp[i][j]=mp[j][i]=fun(x[i],y[i],x[j],y[j]);
            }
        }


    dp[target]=0;
        dfs(1);

        printf("Case #%d: %.2f\n",ca++,dp[1]);

    }
    return 0;
}

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <string>
#include <cmath>
#define INF 999999999
using namespace std;
int n;
double dp[(2<<22)+10];
double x[32],y[32];
double mp[50][50];
int vis[(2<<22)+10];
int target;

double fun(double a,double b,double c,double d)
{
    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}

int main()
{
int t;
int ca=1;
    double sx,sy;
        scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf",&sx,&sy);

        scanf("%d",&n);
        target=(1<<(2*n))-1;

        x[2*n]=sx;y[2*n]=sy;

        memset(mp,0,sizeof mp);

        for(int i=0;i<2*n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
            mp[i][2*n]=mp[2*n][i]=fun(x[i],y[i],x[2*n],y[2*n]);

            for(int j=0;j<i;j++)
                mp[i][j]=mp[j][i]=fun(x[i],y[i],x[j],y[j]);
        }

        for(int i=0;i<=target;i++)
            dp[i]=INF;

            dp[0]=0;
        int j;
        for(int i=0;i<=target;i++)
        {
            if(dp[i]==INF) continue;

            for(j=0;j<2*n;j++)
                if((i&(1<<j))==0) break;

                for(int k=j+1;k<2*n;k++)
                {
                    if(i&(1<<k)) continue;
                    int ff=i+(1<<j)+(1<<k);
                double tmp=min(mp[2*n][k],mp[2*n][j]);
                       tmp+=mp[k][j];
                    dp[ff]=min(dp[ff],dp[i]+tmp);
                }
        }

        printf("Case #%d: %.2f\n",ca++,dp[target]);

    }
    return 0;
}










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值