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
2011 Multi-University Training Contest 9 - Host by BJTU
 

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;
}











你可能感兴趣的:(struct,Integer,UP,each,output,Training)