uva 10123 No Tipping (状压dp)
来源:互联网 发布:2009年好听的网络歌曲 编辑:程序博客网 时间:2024/05/19 09:36
The challenge is to keep the lever balanced while adjusting the objects on it. Assume you have a straight, evenly weighted board, 20 meters long and weighing three kilograms. The middle of the board is the center of mass, and we will call that position 0. So the possible positions on the board range from -10 (the left end) to +10 (the right end). The board is supported at positions -1.5 and +1.5 by two equal fulcrums, both two meters tall and standing on a flat floor. On the board are six packages, at positions -8, -4, -3, 2, 5 and 8, having weights of 4, 10, 10, 4, 7 and 8 kilograms, respectively as in the picture below.
You are to write a program which solves problems like the one described above. The input contains multiple cases. Each case starts with three integers: the length of the board (in meters, at least 3), the weight of the board (in kilograms) and n the number of packages on the board (n <= 20). The board is supported at positions -1.5 and +1.5 by two equal fulcrums, both two meters tall and standing on a flat floor. The following n lines contain two integers each: the position of a package on board (in meters measured from the center, negative means to the left) and the weight of the package (in kilograms). A line containing three 0's ends the input. For each case you are to output the number of the case in the format shown below and then n lines each containing 2 integers, the position of a package and its weight, in an order in which the packages can be removed without causing the board to tip. If there is no solution for a case, output a single line Impossible. There is no solution if in the initial configuration the board is not balanced.
Sample input
20 3 6-8 4-4 10-3 102 45 78 820 3 151 10 8 5-6 85 9-8 48 10-3 10-4 52 9-2 23 3-3 25 1-6 12 530 10 2-8 1009 910 0 0Possible Output for sample input
Case 1:-4 108 8-8 45 7-3 102 4Case 2:1 10 8 5-6 85 9-8 48 10-3 10-4 52 9-2 23 3-3 25 1-6 12 5Case 3:Impossible普通的状压dp,不过写搓了,卡时间过的。看了解题报告后,发现经过分析可以有各种优化剪枝。好吧。。。
(uva的题就暂时练到这儿吧,后面抓紧练一下红书上的网络流和匹配,不能坑队友啊!)
#include<cstdio>#include<map>#include<queue>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<list>#include<set>#include<cmath>using namespace std;const int maxn = 20 + 5;const int INF = 1e9;const double eps = 1e-6;typedef unsigned long long ULL;typedef long long LL;typedef pair<int, int> P;#define pos first#define w secondint len, totalw, n;P p[maxn];int dp[1<<maxn];const double posl = -1.5, posr = 1.5;double der;P track[1<<maxn];int solve(int x){ if(dp[x] != -1) return dp[x]; double wlposl = 0, wlposr = 0, wrposr = 0, wrposl = 0; for(int i = 0;i < n;i++){ if((1<<i)&x){ if(p[i].pos < posl) wlposl += (posl-p[i].pos)*p[i].w; if(p[i].pos < posr) wlposr += (posr-p[i].pos)*p[i].w; if(p[i].pos > posl) wrposl += (p[i].pos-posl)*p[i].w; if(p[i].pos > posr) wrposr += (p[i].pos-posr)*p[i].w; } } if(wlposl > wrposl+der || wlposr+der < wrposr) return dp[x] = 0; int ret = 0; for(int i = 0;i < n;i++){ if((1<<i)&x){ if(p[i].pos < posl){ if(wrposr < wlposr-(posr-p[i].pos)*p[i].w + der && solve(x-(1<<i))){ ret = 1; track[x] = P(x-(1<<i), i); } } else if(p[i].pos < posr){ if(wlposl < wrposl-(p[i].pos-posl)*p[i].w+der && wrposr < wlposr-(posr-p[i].pos)*p[i].w+der && solve(x-(1<<i))){ ret = 1; track[x] = P(x-(1<<i), i); } } else{ if(wlposl < wrposl-(p[i].pos-posl)*p[i].w+der && solve(x-(1<<i))){ ret = 1; track[x] = P(x-(1<<i), i); } } } } return dp[x] = ret;}int main(){ int kase = 0; while(scanf("%d%d%d", &len, &totalw, &n)){ kase++; if(len == 0 && totalw == 0 && n == 0) break; der = 1.5*totalw; for(int i = 0;i < n;i++) scanf("%d%d", &p[i].pos, &p[i].w); sort(p, p+n); memset(dp, -1, sizeof dp); dp[0] = 1; int ans = solve((1<<n)-1); printf("Case %d:\n", kase); if(ans){ int now = (1<<n)-1; int cnt = 0; while(1){ cnt++; if(cnt > 100) break; if(now == 0) break; P tem = track[now]; now = tem.pos; printf("%d %d\n", p[tem.w].pos, p[tem.w].w); } } else puts("Impossible"); } return 0;}/*5 5 10 3*/
- uva 10123 No Tipping (状压dp)
- uva 10123 - No Tipping
- UVa 10123 - No Tipping
- uva 10123 No Tipping
- uva 10123 No Tipping
- UVA 10123 - No Tipping
- UVA 10123 No Tipping
- UVA 10123 No Tipping
- UVa 10123 - No Tipping
- UVA 10123 No Tipping
- uva 10123 No Tipping(DFS+几何力矩+贪心)
- UVA 10123 No Tipping (物理+贪心+DFS剪枝)
- uva :10123 - No Tipping(dfs + 几何力距 )
- UVA - 10123 No Tipping(回溯+剪枝+贪心)
- uva 10123 No Tipping(逆向思维+力矩)
- UVA - 10123 No Tipping 剪枝+dfs
- UVA No Tipping
- UVa10123 No Tipping
- RailsCasts17 HABTM Checkboxes 更新多对多的关系
- NSBundle的使用方法
- GlassFish 添加 hibernate JPA
- Linux 线程模型的比较:LinuxThreads 和 NPTL
- hdu 4781 /构造 出 符合要求的图
- uva 10123 No Tipping (状压dp)
- mybaits的xxMapper.xml文件中大于号和小于号处理
- 手把手教你写Linux I2C设备驱动
- autotools实现makefile的自动生成
- IOS笔记—判断邮箱格式是否正确的代码
- Mule Esb实现WebService代理
- 接口和抽象类有什么区别
- IOS开发笔记--图片压缩
- android layout将Imageview放在最上层