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 10 2 4 5 7 8 8 20 3 15 1 10 8 5 -6 8 5 9 -8 4 8 10 -3 10 -4 5 2 9 -2 2 3 3 -3 2 5 1 -6 1 2 5 30 10 2 -8 100 9 91 0 0 0Possible Output for sample input
Case 1: -4 10 8 8 -8 4 5 7 -3 10 2 4 Case 2: 1 10 8 5 -6 8 5 9 -8 4 8 10 -3 10 -4 5 2 9 -2 2 3 3 -3 2 5 1 -6 1 2 5 Case 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 second int 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 1 0 3 */
杠杆平衡问题的程序求解
博客围绕杠杆平衡问题展开,给定特定长度、重量的杠杆及多个物体的位置和重量,需编写程序解决此类问题。输入包含多组数据,每组含杠杆信息和物体信息,输出为可移除物体的顺序,若初始不平衡则输出 Impossible。
275

被折叠的 条评论
为什么被折叠?



