HDU 1574 RP Problem __ Dynamic Programming base DP

Source: Internet
Author: User

The problem: Each thing has three attributes, moral value A, threshold B, benefit C, if a>0, less than this threshold B can do this thing, the income C is negative, conversely, if the a<0, greater than this threshold B can do, income C is positive.

Thought: Because the character value can be positive or negative, unified plus 10000 are treated as positive, the definition of dp[n] for, the character value of n when the greatest benefit, here need to mark a person if the addition of this, the product value of n has appeared, not directly updated, there is the optimal solution.


#include <fstream> #include <iostream> #include <string> #include <cstring> #include < complex> #include <math.h> #include <set> #include <vector> #include <map> #include <queue > #include <stdio.h> #include <stack> #include <algorithm> #include <list> #include <ctime > #include <memory.h> #include <ctime> #include <assert.h> #define REP (i,a,n) for (int i=a;i<n;i+ +) #define (i,a,n) for (int i=n-1;i>=a;i--) #define PB push_back #define MP Make_pair #define ALL (x) (x). Begin (), (x)
. End () #define FI #define SE second #define EPS 1e-8 #define M_PI 3.141592653589793 typedef long LL;
const LL mod=1000000007;
const int INF=0X7FFFFFFF; ll Powmod (ll A,ll b) {ll res=1;a%=mod;for (; b;b>>=1) {if (b&1) res=res*a%mod;a=a*a%mod;}
return res;}
int dp[21000],v[21000];
int a[1010],b[1010],c[1010];
using namespace Std;
    int main () {Ios::sync_with_stdio (false); int T;cin>>t;
      while (t--) {int n;cin>>n;
      for (int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i];
      Memset (Dp,0,sizeof (DP));
      memset (v,0,sizeof (v));
      V[10000]=1;
                    for (int i=0;i<n;i++) {if (a[i]>0) {for (int j=10000+b[i];j>=0;j--) {if (v[j)) {
                      if (!v[j+a[i]]) {v[j+a[i]]=1;
                    Dp[j+a[i]]=dp[j]+c[i];
                    } else{Dp[j+a[i]]=max (Dp[j+a[i]],dp[j]+c[i]);
                else{for (int j=10000+b[i];j<=20010;j++) {
                       if (V[j]) {if (!v[j+a[i]]) {v[j+a[i]]=1;
                     Dp[j+a[i]]=dp[j]+c[i];
                    } else{Dp[j+a[i]]=max (Dp[j+a[i]],dp[j]+c[i]);
           }
                }  {}} int res=0;
      for (int i=0;i<20100;i++) Res=max (Res,dp[i]);
    cout<<res<<endl;
 }
}

Winter training has been faster than a half, road long its repair far XI, I will go up and down and search!

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.