DP (I, j, K) indicates that the first I type of coin is considered (small to large), Alice's Money is J, Bob's money is K, the minimum number of times. Brain repair can be found, only A->B.C, B->A.C, c->a.b, A.b->c, A.c->b, B.c->a 6 situation, enumeration and then dp a bit OK. DP with a brush table, there is a strong pruning is after the coin regardless of the combination can not be satisfied when not to update.
--------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1009;const int INF = 0X3F3F3F3F;const int n = 6;const int m[] = {1, 5, ten , +, +);const int g[] = {1, 5, ten, Ten, +};int DP[2][MAXN][MAXN];int a[n], b[n], c[n], x, y, z, N;int AS, BS, CS, at, BT, CT;void Read (int c[], int &v) {v = 0;for (int i = n; i--;) {scanf ("%d", C + i);v + = c[i] * M[i];}}void Init () {scanf ("%d%d%d", &x, &y, &z);Read (A, as);Read (B, BS);Read (C, CS);At = as + z-x;BT = bs + x-y;ct = cs + y-z;N = as + BS + CS;}inline void upd (int &x, int t) {if (t < x) x = t;}void Work () {int c = 0, p = 1;memset (DP, INF, sizeof DP);Dp[c][as][bs] = 0;for (int t = 0; t < N; t++) {Swap (c, p);memset (Dp[c], INF, sizeof dp[c]);for (int i = 0; I <= N; i++)For (int j = 0; i + j <= N; j + +) {int k = N-i-J, &v = Dp[p][i][j];if (V = = INF | | (at-i)% g[t] | | (bt-j)% g[t] | | (ct-k)% g[t])continue;upd (Dp[c][i][j], V);//A->b,cfor (int _a = 0; _a <= a[t]; _a++)for (int _b = 0; _b <= _a; _b++)upd (Dp[c][i-_a * m[t]][j + _b * m[t]], V + _a);//B->a,cfor (int _b = 0; _b <= b[t]; _b++)for (int _a = 0; _a <= _b; _a++)upd (dp[c][i + _a * m[t]][j-_b * m[t]], V + _b);//c->a,bfor (int _c = 0; _c <= c[t]; _c++)for (int _a = 0; _a <= _c; _a++)upd (dp[c][i + _a * m[t]][j + (_c-_a) * M[t]], V + _c);//A,b->cfor (int _a = 0; _a <= a[t]; _a++)for (int _b = 0; _b <= b[t]; _b++)upd (Dp[c][i-_a * m[t]][j-_b * m[t]], V + _a + _b);//A,c->bfor (int _a = 0; _a <= a[t]; _a++)for (int _c = 0; _c <= c[t]; _c++)upd (Dp[c][i-_a * m[t]][j + (_a + _c) * M[t]], V + _a + _c);//b,c->afor (int _b = 0; _b <= b[t]; _b++)for (int _c = 0; _c <= c[t]; _c++)upd (dp[c][i + (_b + _c) * m[t]][j-_b * m[t]], V + _b + _c);}}if (DP[C][AT][BT]! = INF)printf ("%d\n", DP[C][AT][BT]);Elseputs ("impossible");}int main () {Init ();Work ();return 0;}
--------------------------------------------------------------------------
1021: [Shoi2008]debt cycle of debt time limit: 1 Sec Memory Limit: 162 MB
Submit: 735 Solved: 387
[Submit] [Status] [Discuss] Description
Alice, Bob and Cynthia always fret over their troubled debts, and one day they decided to sit down together to solve the problem. However, it is a very troublesome matter to identify the authenticity of banknotes, so they decide to exchange cash as little as possible when they are repaid. For example, Alice owes Bob 10 Yuan, while Cynthia and the two don't owe each other. Now suppose Alice has only one 50 yuan, Bob has 3 10 Yuan and 10 Zhang 1 yuan, Cynthia 3 Yuan 20. A more straightforward approach is that Alice will give $50 to Bob, and Bob will find his money to Alice, so there will be a total of 14 banknotes exchanged. But this is not the best approach, the best thing is: Alice put 50 to Cynthia,cynthia and then two 20 to Alice, another 20 to Bob, and Bob put a 10 to C, at this time only 5 banknotes were exchanged. It wasn't long before they found out that it was a tricky problem, so they found a math-savvy you to solve the problem.
Input
The first line of input includes three integers: x1, x2, X3 ( -1,000≤x1,x2,x3≤1,000), where X1 represents the Money Alice owes Bob (if X1 is negative, it means Bob owes Alice's money) X2 on behalf of Bob owes Cynthia Money (if X2 is a negative number, stating that Cynthia owes Bob Money) X3 represents Cynthia owes Alice's Money (if X3 is negative, stating that Alice owes Cynthia Money) Next there are three lines, each line includes 6 natural numbers: A100,A50,A20,A10,A5,A1 b100,b50,b20,b10,b5,b1 c100,c50,c20,c10,c5,c1 A100 says Alice has a number of $100 banknotes, and B50 represents Bob's 50-dollar bill Count, And so on In addition, we guarantee a a10+a5+a1≤30,b10+b5+b1≤30,c10+c5+c1≤30, and the total value of all three banknotes will not exceed 1,000.
Output
If the debt can be repaid, the output needs to exchange the minimum number of banknotes, and if not, output "impossible" (note that the words are all lowercase, not quoted when outputting to a file).
Sample InputEnter a
10 0 0
0 1 0 0 0 0
0 0 0 3 0 10
0 0 3 0 0 0
Input Two
-10-10-10
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0Sample OutputOutput A
5
Output two
0HINT
For 100% of data, X1, x2, x3≤|1,000|.
Source
Bzoj 1021: [Shoi2008]debt cycle of debt (DP)