01 + backpack sort (memory search) Proud businessman (HDU - 3466)

01 + backpack sort

Topic connection: http://acm.hdu.edu.cn/showproblem.php?pid=3466

Subject to the effect: n commodity, m dollars, each product has a p, q, v attributes, p price, q represents buy this product you need to bring q yuan boss was willing to trade with you, this commodity actual v value, inquired how much value you can get the most.

This problem is a prerequisite of dp, will lead did not reach the limits of j reached after i + 1, but can not update properly, can not directly address this problem, it is necessary to use sorting to resolve. For example, for the second sample, the first item 5105 will only dp [10] to update it, but in fact took 5, when updating the second article, the need dp [10] = max (dp [10 -5] + 6, dp [10]), this time need by the upper hierarchy dp [5], but in fact at this time dp [5] has not been updated.

Discover first whether the value of v, if there are two items 510 and 1012, then in order to buy up the order will affect the money needed. Needs money for each Q2 + P1 = 17 and Q1 + P2 = 20, selected from the need certainly less money that Qx + Py, i.e. to buy when y Q2 + P1> Q1 + P2, can be reduced to Q2-P2> Q1- P1, that is, Qx - Px large consider buying.

Also, remember to use this choice to search dfs direct simulation, if the loop iteration, will have to reverse the cycle, can also be directly sorted according to Qx + Py grew up playing big order.

Memory search wording:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int dp[555][5555];
 7 int N,M;
 8 typedef struct{
 9     int q,p,v;
10 }node;
11 node a[555];
12 bool cmp(node a,node b){
13     return a.q-a.p>b.q-b.p;
14 }
15 int DP(int i,int j){   
16     if(dp[i][j]>=0) return dp[i][j];
17     int res;
18     if(i==N) return 0;
19     
20     else if(j<a[i].q){
21         res=DP(i+1,j);
22     }
23     else{
24           res=max(DP(i+1,j),DP(i+1,j-a[i].p)+a[i].v);    
25     }
26     return dp[i][j]=res;
27 }
28 
29 int main(){
30     while(scanf("%d%d",&N,&M)!=EOF){
31         memset(dp,-1,sizeof(dp));
32 //        memset(dp,0,sizeof(dp));
33         for(int i=0;i<N;i++){
34             scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
35         }
36         sort(a,a+N,cmp);
37 //        for(int i=0;i<N;i++){
38 //            for(int j=0;j<=M;j++){
39 //                if(j<a[i].q) 
40 //                  dp[i+1][j]=dp[i][j];
41 //                else 
42 //                  dp[i+1][j]=max(dp[i][j-a[i].p]+a[i].v,dp[i][j]);
43 //            }
44 //        }
45         printf("%d\n",DP(0,M));
46     }
47 }
View Code

Iterative writing:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int dp[555][5555];
int N,M;
typedef struct{
    int q,p,v;
}node;
node a[555];
bool cmp(node a,node b){
    return a.q-a.p<b.q-b.p;
}
//int DP(int i,int j){   
//    if(dp[i][j]>=0) return dp[i][j];
//    int res;
//    if(i==N) return 0;
//    
//    else if(j<a[i].q){
//        res=DP(i+1,j);
//    }
//    else{
//          res=max(DP(i+1,j),DP(i+1,j-a[i].p)+a[i].v);    
//    }
//    return dp[i][j]=res;
//}

int main(){
    while(scanf("%d%d",&N,&M)!=EOF){
//        memset(dp,-1,sizeof(dp));
        memset(dp,0,sizeof(dp));
        for(int i=0;i<N;i++){
            scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
        }
        sort(a,a+N,cmp);
        for(int i=0;i<N;i++){
            for(int j=0;j<=M;j++){
                if(j<a[i].q) 
                  dp[i+1][j]=dp[i][j];
                else 
                  dp[i+1][j]=max(dp[i][j-a[i].p]+a[i].v,dp[i][j]);
            }
        }
        printf("%d\n",dp[N][M]);
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/-Zzz-/p/11409490.html