Poj 1276 cash machine

Source: Internet
Author: User

Question: give you a given amount of cash goal, and you have n kinds of cash.

The next step is how much cash you own with N denominations. You need to find the maximum value that can be obtained by using cash in your hand for a given goal!


Thought: Multi-backpack problem, state equation dp (I, V) = max (dp (I, j), DP (I-1, V-KCI) + KWI );

After optimization, the AC code is as follows:

# Include <stdio. h> # include <string. h> struct P {int N; int D;} num [11]; int DP [100010]; int main () {int I, j, N, goal, K, cmax; while (scanf ("% d", & goal, & N )! = EOF) {for (I = 0; I <n; I ++) scanf ("% d", & num [I]. n, & num [I]. d); memset (DP, 0, sizeof (DP); DP [0] = 1; // C starts from 0 Cmax = 0; for (I = 0; I <n; I ++) for (j = Cmax; j> = 0; j --) if (DP [J]) // If for (k = 1; k <= num [I]. n; k ++) {int temp = J + K * num [I]. d; If (temp> goal) break; DP [temp] = 1; // mark from the current position and search continuously if (temp> Cmax) Cmax = temp ;} printf ("% d \ n", Cmax);} 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.