Poj 1276 cash machine (multiple backpacks)

Source: Internet
Author: User

Link: poj 1276

Question: The known amount cash.Number and nominal value of currencies with the same nominal value,You can use the given currency to generate

Less than or equal to the maximum cash amount

Analysis: Because the nominal value and quantity of each currency are known, it can be convertedMultiple backpacks,The capacity of the backpack is cash,

The value and cost of each item are the nominal values of each currency.

Multiple backpacks can be converted to 01 backpacks, but this will time out. To avoid this, it can be convertedFull backpack and binary thought 01 backpack

# Include <stdio. h> # include <string. h> int f [100010], V; int max (int A, int B) {return A> B? A: B;} void zeroone (INT cost, int weight) // 01 backpack {int I; for (I = V; I> = cost; I --) f [I] = max (F [I], F [I-cost] + weight);} void complete (INT cost, int weight) // complete backpack {int I; for (I = cost; I <= V; I ++) f [I] = max (F [I], F [I-cost] + weight );} void multiple (INT num, int cost, int weight) // multiple backpacks {int K; If (Num * weight> = V) {complete (cost, weight); return ;} for (k = 1; k <num; K * = 2) {zeroone (K * cost, K * weight); num-= K;} zeroone (Num * cost, num * Weight);} int main () {int I, n, num [1010], W [1010]; while (scanf ("% d", & V )! = EOF) {scanf ("% d", & N); for (I = 1; I <= N; I ++) scanf ("% d ", & num [I], & W [I]); memset (F, 0, sizeof (f); for (I = 1; I <= N; I ++) multiple (Num [I], W [I], W [I]); printf ("% d \ n", F [v]);} return 0 ;}

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.