3-dimensional dynamic planning of the HDU 4501 series of Xiao Ming stories-Purchasing New Year's goods

Source: Internet
Author: User

 

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4501

 

Question:

There are a total of N items, each of which has a purchase price, purchase points, and score value.

There are K items available for purchase, with v1 yuan and V2 points. Ask the maximum score value.

 

Solution:

DP [I] [J] [k] indicates the opportunity to have I yuan, J points, and K free items, and the maximum score value.

There are four options for each item: 1. Buy with money 2. Use points for 3. Use a free item for 4. Do not use anything

 

Code:

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <queue> # define EPS 1e-6 # define Inf (1 <20) # define PI ACOs (-1.0) using namespace STD; int DP [110] [110] [10]; // DP [I] [J] [k] indicates the maximum value struct thing {int A, B, Val ;} thing [110]; int main () {int N, V1, V2, K; while (scanf ("% d % D ", & N, & V1, & V2, & K )! = EOF) {int sum = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d ", & thing [I]. a, & thing [I]. b, & thing [I]. val); sum + = thing [I]. val;} memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) for (INT q = K; q> = 0; q --) for (Int J = V1; j> = 0; j --) for (int p = V2; P> = 0; p --) {If (j> = thing [I]. A & P> = thing [I]. b) // If you can use credit or money {DP [J] [p] [Q] = max (DP [J-thing [I]. a] [p] [Q] + thing [I]. val, DP [J] [p-thing [I]. b] [Q] + thing [I]. val), DP [J] [p] [Q]); If (q> = 1) // If you can use free quota DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val);} else if (j <thing [I]. A & P <thing [I]. b) // If neither money nor points can be used {If (q> = 1) DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val);} else if (j <thing [I]. a) // only points can be used {DP [J] [p] [Q] = max (DP [J] [p-thing [I]. b] [Q] + thing [I]. val, DP [J] [p] [Q]); If (q> = 1) DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val);} else // only pay {DP [J] [p] [Q] = max (DP [J-thing [I]. a] [p] [Q] + thing [I]. val, DP [J] [p] [Q]); If (q> = 1) DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val) ;}} printf ("% d \ n", DP [V1] [V2] [k]);} 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.