HDU 3920 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): 722 Accepted Submission(s): 217


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


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

double dis[21][21];
double f[1 << 20];
int n;
struct LPoint{
	double x, y;
}point[21];

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

double imin(double a, double b){
	return a < b ? a : b;
}

double dfs(int stat){
	int i, j, t;
	double k;
	if (!stat) return 0;
	if (f[stat] >= 0) return f[stat];
	
	t = stat;
	for (i = 0, k = 0x7fffffff; t; i++, t >>= 1){
		if (!(t & 1)) continue;
		t >>= 1;
		for (j = i + 1; t; j++, t >>= 1){
			if (!(t & 1)) continue;
			k = imin(k, dis[i + 1][j + 1] + imin(dis[0][i + 1], dis[0][j + 1]) + dfs(stat - (1 << i) - (1 << j)));
		}
		break;
	}
	return f[stat] = k;
}

int main(){
	int i, j, k, T;
	// freopen("A.in","r",stdin);
	// freopen("out.txt","w",stdout);
	scanf("%d", &T);
	for (k = 1; k <= T; k++){
		scanf("%lf %lf", &point[0].x, &point[0].y);
		scanf("%d", &n);
		for (i = 1; i <= n + n; i++)
			scanf("%lf %lf", &point[i].x, &point[i].y);
		for (i = 0; i <= n + n; i++)
			for (j = 0; j <= i; j++){
				if (i == j) dis[i][j] = 0;
				else dis[i][j] = dis[j][i] = Dist(point[i], point[j]);
			}
		for (i = 1; i < (1 << (n + n)); i++) f[i] = -1; 
		printf("Case #%d: %0.2lf\n", k, dfs((1 << (n + n)) - 1));
	}
	return 0;
}

/*************************
看到数据范围,可以用状态dp
f[s] 状态s的最小花费
f[s] = min{f[s - {i,j}] + cost(i,j)}
其中cost(i,j) = dis[i][j] + min(dis[0][i], dis[0][j]);
开始我真乖乖的枚举状态s中,最后射中的那俩数,果断超时...
后经范大哥讲解,只要枚举一个就好了,另一个随便取一个就行,不会丢情况。
可以这么想,随便取一个当最后射中的(射中顺序其实无所谓的,只是易于理解),此时只需枚举另一个和它一块射中的,就行了

总结:用递推的方法实现,总是TLE,换做记忆化搜索就快很多,想不太明白...
可能记忆化搜索把奇数个1的状态省去了
常数级差距在卡时时真让人郁闷= =
*************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值