[DP] topics - traveling salesman backpack

Link: https: //www.luogu.org/problemnew/show/P1782

Personally, this question is a very good multi-practiced hand and a backpack full title.

Title Description

Small S believes all problems can be solved in polynomial time, so he was ready to go back when a traveling salesman. Before departure, he purchased some items. There are n types of these items, the i th volume Vi, the value of Wi, Di total member. His backpack volume is C. How to dress to get as much yield? Big Ben as a god, he was easy to solve this problem.

However, before his departure, he received a number of goods on. These goods there are m items, the relationship between the value of the volume of distribution Yi and Xi is the i-th element: Yi = ai * Xi ^ 2 + bi * Xi + ci. This is a good thing, but the small S do not know how to deal with, so he found a super god Ben (that's you), please help him solve this problem.

Input and output formats

Input formats:

 

The first row of three numbers n, m, C, as described in the title;

The following n rows, each row having three number Vi, Wi, Di, as in the title;

The m rows, each row having three numbers ai, bi, ci, as described in the title.

 

Output formats:

 

Only one line, for the greatest value.

Since talking about the backpack, then we come to a few basic backpack review again:

1.01 Backpack:

  Needless to say, 01 backpack is for an item, you choose or not choose, choose the state by that state before you choose transferred from.

  Fake code:

for:(i) 1->n
    for: (j) maxv->v[i]
        f[j]=max ( f[j-v[i]]+w[i] )

2. Fully backpack:

  Fully taking into account the backpack can hold an unlimited number of each item, but in essence, the difference between 01 and backpacks is that the state transition not only from the transfer from the state before the election, may also be a state after the election (this is equivalent to repeat selection), the second layer being a write cycle to:

 

for:(i) 1->n
    for: (j) v[i]->maxv
        f[j]=max ( f[j-v[i]]+w[i] )

 

3. multiple backpacks:

  Multiple backpack limits the number of items, so items can not be increased based on the original, but the other from a cycle, the number of enumeration.

 

for:(i) 1->n
    for: (j) maxv->v[i]
        for:(k) 1->c[i] (k*v[i]<=j)
            f[j]=max ( f[j-k*v[i]]+k*w[i] )

 

The rest of the backpack to say later. In fact, it is too lazy to write

 

So then there is the idea of ​​it!

This question is a little deceptive data but do we need to do some optimization on the foundation.

- monotonous queue:

Consider a queue to optimize the monotonous:

See sliding window

 

We maintain the most value, the queue can be used to optimize monotonous.

 

- read optimization (nonsense)

#include<iostream>
#include<cstdio>
using namespace std;
const int N=10010;
int n,m,cool;
int v[N],w[N],d[N];
int a[10],b[10],c[10];
int tietie[N];
int fu[N],sai[N];
int main(){
    scanf("%d%d%d",&n,&m,&cool);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&v[i],&w[i],&d[i]);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&a[i],&b[i],&c[i]);
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<v[i];j++){
            for(int k=0,bu=0,ki=0;k*v[i]+j<=cool;k++){
                int matsuri=tietie[j+k*v[i]]-k*w[i];
                while(bu!=ki&&matsuri>fu[ki-1]) ki--;
                sai[ki]=k,fu[ki++]=matsuri;
                if(sai[bu]+d[i]<k) bu++;
                fu[k*v[i]+j]=fu[bu]+k*w[i];
            }
        }
    }
    for(int i=1;i<=m;i++){
        for(int j=cool;j>=0;j--){
            for(int k=0;k<=j;k++){
                if(tietie[j]<tietie[j-k]+(k*a[i]+b[i])*k+c[i]) tietie[j]=tietie[j-k]+(k*a[i]+b[i])*k+c[i];
            }
        }
    }
    printf("%d",tietie[cool]);
    return 0;
} 

奇怪的是没有过,所以暂时放这了,以后再改。

 

 

Guess you like

Origin www.cnblogs.com/Nelson992770019/p/11240495.html