HDU5410 (more than 2015 schools)--CRB and his Birthday (knapsack problem)

Source: Internet
Author: User

Topic Link: Click to open the link

The main topic: there is M money, there are n kinds of goods, each kind of goods have a WI price, suppose to buy x i in the goods, will spend wi*x money, at the same time the boss will also give A*x+b candy, ask how to buy gifts, can let candy number the most.

Dp[i][j][0] When I was bought, the money was J and I could get the most candy number without buying it.

DP[I][J][1] When I was bought, the money was J and bought the most candy I could get.

By the number of goods traversed from 1 to N, then you can omit one dimension, dp[i][0] in the current item, spend money for J not buy the current maximum value of this item, Dp[i][1] in the current item, spend money for J and buy the maximum value of the current item.

Then the state transfer equation has been

DP[I+W][1] = max (dp[i+w][1],dp[i][0]+a+b);
DP[I+W][1] = max (dp[i+w][1],dp[i][1]+a);

Note When updating an item dp[i] = MAX (dp[i][0],dp[i][1]); DP[I][1] is 0

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; int dp[2010][2]; int main () {    int t, N, M, W, a, B, ans, I;    scanf ("%d", &t);    while (t--) {        scanf ("%d%d", &m, &n);        Ans = 0;        Memset (Dp,0,sizeof (DP));        while (n--) {            scanf ("%d%d%d", &w, &a, &b);            for (i = 0; I <= m; i++) {                if (i+w <= m) {                    dp[i+w][1] = max (dp[i+w][1],dp[i][0]+a+b);                    DP[I+W][1] = max (dp[i+w][1],dp[i][1]+a);                }                Dp[i][0] = max (dp[i][0],dp[i][1]);                DP[I][1] = 0;                ans = max (ans,dp[i][0]);            }        }        printf ("%d\n", ans);    }    return 0;}


Copyright NOTICE: Reprint Please specify source: Http://blog.csdn.net/winddreams

Hdu5410 (more than 2015 schools)--CRB and his Birthday (knapsack problem)

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.