Bzoj_1021_[shoi2008]_debt Cycle of debt _ (DP)

Source: Internet
Author: User

Describe

http://www.lydsy.com/JudgeOnline/problem.php?id=1021

Three of people owe money to each other, give each of their various denominations of the number of bills each, the minimum need to pass the number of banknotes to pay off the account.

Analysis

Use \ (f[i][j][k]\) to indicate the use of the former \ (i\) type of banknotes, A has \ (j\) yuan, B has \ (K\) The number of steps required.

Then the DP will be fine.

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= ++5, inf=~0u>>1;5 intsum,t;6 intx[3],h[3],g[3],m[6]={ -, -, -,Ten,5,1},num[6];7 intcnt[3][6];8 intf[2][MAXN][MAXN];9Inlinevoidsolve () {Ten     if(g[0]<0|| g[1]<0|| g[2]<0) {puts ("Impossible");return; } One      for(intj=0; j<maxn;j++) for(intk=0; k<maxn;k++) f[0][j][k]=f[1][j][k]=INF; Af[1][h[0]][h[1]]=0; -      for(intI=0;i<6; i++){ -          for(intj=0; j<maxn;j++) for(intk=0; k<maxn;k++) f[t][j][k]=INF; the          for(intj=0; j<=sum;j++) for(intk=0; k+j<=sum;k++){ -             if(f[t^1][j][k]<INF) { -                  for(intA=0; a<=num[i];a++) for(intb=0; a+b<=num[i];b++){ -                     intta=j+m[i]* (a-cnt[0][i]), tb=k+m[i]* (b-cnt[1][i]); +                     if(ta>=0&&tb>=0&&ta+tb<=sum) { -                         intD= (ABS (cnt[0][I]-A) +abs (cnt[1][i]-b) +abs (cnt[2][I]-NUM[I]+A+B))/2; +F[t][ta][tb]=min (f[t][ta][tb],f[t^1][j][k]+d); A                     } at                 } -             } -         } -t^=1; -     } -     if(f[t^1][g[0]][g[1]]==inf) puts ("Impossible"); in     Elseprintf"%d\n", f[t^1][g[0]][g[1]]); - } toInlinevoidinit () { +scanf"%d%d%d", &x[0],&x[1],&x[2]); -      for(intI=0;i<3; i++) for(intj=0;j<6; j + +) scanf ("%d", &cnt[i][j]), G[i]+=cnt[i][j]*m[j], h[i]+=cnt[i][j]*m[j], sum+=cnt[i][j]*m[j], num[j]+=Cnt[i][j]; the      for(intI=0;i<3; i++) G[i]-=x[i], g[(i+1)%3]+=X[i]; * } $ intMain () {Panax Notoginseng init (); - solve (); the     return 0; +}
View Code

1021: [Shoi2008]debt cycle of debt time limit:1 Sec Memory limit:162 MB
submit:832 solved:432
[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, Al
Ice owes Bob 10 Yuan, while Cynthia and the two don't owe each other. Now suppose Alice has only one $50, Bob has 3 10 Yuan and 10 tickets 1 yuan, Cynthia has 3
Zhang 20 Yuan. A more straightforward approach is that Alice will give $50 to Bob, and Bob will give Alice the money he has, so there will be 14 banknotes altogether.
Be exchanged. But this is not the best approach, the best thing is: Alice put 50 to Cynthia,cynthia and then two 20 to Alice, and another 20 to
Bob, and Bob gave a 10 to C, when only 5 banknotes were exchanged. It wasn't long before they found out that it was a tricky problem, so they looked for
By the math-proficient you solve this problem for them.

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 (e.g.
The X1 is a negative number, which means Bob owes Alice money) X2 on behalf of Bob owes Cynthia Money (if X2 is negative, the Cynthia owes Bob Money) x3
Represents Cynthia owes Alice's Money (if X3 is negative, it means Alice owes Cynthia money)
And then there's three lines.
Each row consists of 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 $100 bill number, B50 says Bob has a 50-dollar bill number, 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
More than 1,000.

Output

If the debt can be repaid, the output needs to be exchanged for the minimum number of banknotes, and if not repaid, the output "impossible" (note the words all
lowercase, do not quote when outputting to 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 Recurring Debt _ (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.