hdu3920 Clear All of Them I

本文介绍了一个关于使用激光枪射击敌人的问题,目标是最小化射击所需的总能量。通过状态动态规划的方法来解决,并针对传统方法存在的重复计算问题进行了优化。

Clear All of Them I

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


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
 

Source
 

Recommend
xubiao



状态dp。

但是按正常做会tle。

考虑下,先射击第1,3个再射击第2,4个和先射击2,4个再射击1,3个得到的结果是一样的,导致了重复计算。因此,每次选两个没有被射击过的时候,要求其中一个一定是没有被射击的第一个人不会导致重复计算。这样能减少很多状态转移。


代码

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

typedef struct
{
    int x,y;
}Point;

Point a[25];
double dp[(1<<20)+5];
int av[(1<<20)];
int b[(1<<20)+2];

double Dist(Point x,Point y)
{
    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}

double Count(Point x,Point y,Point z)
{
    return Dist(x,y)+Dist(y,z);
}

int main()
{
    int i,j,n,T,k,p,up,t,s,upp,cnt=1;
    Point begin;
    up=0;
    for (i=0;i<(1<<20);i++)
    {
        s=0;
        for (j=0;j<20;j++)
        {
            if ((i & (1<<j))!=0)
            {
                s++;
            }
        }
        if (s%2==0)
        {
            av[up]=i;
            b[i]=up++;
        }
    }
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&begin.x,&begin.y);
        scanf("%d",&n);
        for (i=0;i<2*n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        for (i=0;i<up;i++)
        {
            dp[i]=-1;
            if (av[i]>(1<<2*n)) break;
        }
        upp=i;
        dp[0]=0;
        for (i=0;i<upp;i++)
        {
            if (dp[i]==-1) continue;
            t=av[i];
            for (j=0;j<2*n;j++)
            {
                if ((t & (1<<j))==0) break;
            }
            for (k=j+1;k<2*n;k++)
            {
                if ((t & (1<<k))!=0) continue;
                p=((t | (1<<j)) | (1<<k));
                p=b[p];
                if (dp[p]==-1) dp[p]=min(Count(begin,a[j],a[k]),Count(begin,a[k],a[j]))+dp[i];
                else dp[p]=min(dp[p],min(Count(begin,a[j],a[k]),Count(begin,a[k],a[j]))+dp[i]);
            }
        }
        printf("Case #%d: %.2lf\n",cnt++,dp[i-1]);
    }
    return 0;
}











基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB代码 动态避障路径规划:基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB 融合DWA的青蒿素优化算法(AOA)求解无人机三维动态避障路径规划,MATLAB代码 基于动态环境下多智能体自主避障路径优化的DWA算法研究,MATLAB代码 融合DWA的青蒿素优化算法AOA求解无人机三维动态避障路径规划,MATLAB代码 基于DWA的多智能体动态避障路径规划算法研究,MATLAB代码 融合动态窗口法DWA的粒子群算法PSO求解无人机三维动态避障路径规划研究,MATLAB代码 基于粒子群算法PSO融合动态窗口法DWA的无人机三维动态避障路径规划研究,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于DWA的动态环境下无人机自主避障路径优化,MATLAB代码 基于DWA的动态环境下机器人自主避障路径规划,MATLAB代码 基于城市场景下RRT、ACO、A*算法的无人机三维路径规划方法研究,MATLAB代码 基于城市场景下无人机三维路径规划的导航变量的多目标粒子群优化算法(NMOPSO),MATLAB代码 导航变量的多目标粒子群优化算法(NMOPSO)求解复杂城市场景下无人机三维路径规划,MATLAB代码 原创:5种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 原创:4种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 高维超多目标优化:基于导航变量的多目标粒子群优化算法(NMOPSO)的无人机三维
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值