首页 > 代码库 > HDU3920:Clear All of Them I(状态压缩)
HDU3920:Clear All of Them I(状态压缩)
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
题意:
给出初始坐标,还有目标坐标,一次能射击两个坐标,求路程最短为多少
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; #define exp 1e-8 struct node { double x,y; } o,s[50]; int n; double d[50][50],dp[(1<<20)+1],os[50]; double dis(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int cmp(node a,node b) { return dis(a,o)<dis(b,o); } double dfs(int u) { if(dp[u]>exp) return dp[u]; if(!u) return 0; int m = 0; while(!(u&(1<<m))) m++; int tem; for(int i = m+1; i<n; i++) { if(u&(1<<i)) { tem = u - (1<<i) - (1<<m); double ans = dfs(tem)+os[m]+d[i][m]; if(dp[u]<exp || ans<dp[u]) dp[u] = ans; } } return dp[u]<exp?0:dp[u]; } int main() { int t,i,j,k,cas = 1; scanf("%d",&t); while(t--) { scanf("%lf%lf%d",&o.x,&o.y,&n); n*=2; for(i = 0; i<n; i++) scanf("%lf%lf",&s[i].x,&s[i].y); sort(s,s+n,cmp); for(i = 0; i<n; i++) for(j = 0; j<n; j++) { d[i][j] = dis(s[i],s[j]); } for(i = 0; i<n; i++) os[i] = dis(o,s[i]); for(i = 0; i<(1<<n); i++) dp[i] = -1.0; dfs(i-1); printf("Case #%d: %.2f\n",cas++,dp[i-1]); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。