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.
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.
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
题意:2n个敌人需要杀死,分布在地图上的不同位置,现在有n个炮弹,每次发射能杀死2个人,代价为本人与敌人1距离+敌人1与敌人2距离,求最小的代价;
思路: 压缩状态DP,用一维数组记录从0枚举到(1<<n)-1 满足条件状态的最小值;
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N=0x3f3f3f3f;
struct node{
int x,y;
}str[25],st;
int len;
double d[20];
double dp[1<<20],dist[150][150];
int n;
void solve()
{
memset(dp,0,sizeof(dp));
for(int i=0;i<(1<<2*n);i++)
dp[i]=double(N);
dp[0]=0;
int j;
for(int i=0;i<(1<<2*n);i++)
{
if(dp[i]==N) continue;
for(j=0;j<2*n;j++)
if((i&(1<<j))==0) break;//如果已击杀continue
for(int k=j+1;k<2*n;k++)//j为当前第一个击杀,枚举第二个击杀人
{
if(i&(1<<k))continue;//同上
dp[i|(1<<j)|(1<<k)]=min(dp[i]+min(d[j],d[k])+dist[j][k],dp[i|(1<<j)|(1<<k)]);//状态转移很好理解的
}
}
}
int main()
{
int T;
scanf("%d",&T);
for(int t=1;t<=T;t++)
{
len=0;
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);
dist[i][j]=dist[j][i]=sqrt(x+y);
}
solve();
printf("Case #%d: ",t);
printf("%.2lf\n",dp[(1<<2*n)-1]);
}
return 0;
}