hdu3920Clear All of Them I(预处理加状压dp)

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<cmath>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
    int x,y;
}str[25],st;
double d[25];
double dp[1<<20];
double dis[150][150];
int n;
void solve()
{
    memset(dp,0,sizeof(dp));
    for(int i=0;i<(1<<2*n);i++)
    dp[i]=(double)inf;
    dp[0]=0;
int j;
    for(int i=0;i<(1<<2*n);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;
        dp[i|(1<<j)|(1<<k)]=min(dp[i]+min(d[j],d[k])+dis[j][k],dp[i|(1<<j)|(1<<k)]);
    }

    }
}
int main()
{
    int t,w=0;
    scanf("%d",&t);
    while(t--)
    {w++;
        scanf("%d%d",&st.x,&st.y);
        scanf("%d",&n);
        for(int i=0;i<2*n;i++)
        scanf("%d%d",&str[i].x,&str[i].y);
        for(int i=0;i<2*n;i++)
        {
            int x=(str[i].x-st.x)*(str[i].x-st.x);
            int y=(str[i].y-st.y)*(str[i].y-st.y);
            d[i]=sqrt(x+y);
        }
        for(int i=0;i<2*n;i++)
        for(int j=i+1;j<2*n;j++)
        {
            int x=(str[i].x-str[j].x)*(str[i].x-str[j].x);
            int y=(str[i].y-str[j].y)*(str[i].y-str[j].y);
             dis[i][j]=sqrt(x+y);
        }
        solve();
        printf("Case #%d: %0.2lf\n",w,dp[(1<<2*n)-1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/82939937
今日推荐