luogu P1759 Babel diving

Topic background

· A small road Babel direct Adventures Part III

Title Description

With the help of the Monkey King, a small A finally out of this barren hills, they found a choppy river stopped in front of their own. River did not ship, but fortunately there are n A small diving. Because of his heavy backpack but also back, so he can back m weight of the tool, and because his strength is not infinite, the river is very wide, so he can have the tools back v resistance. But there are very important data under the river, so he wanted to stay for the longest time. So he found you, let him tell you the program.

Input Format

Three numbers m, v, n of said subject such as

Next n lines of three numbers ai, bi, ci, respectively contained gravity, drag, able to support time

Output Format

The first number of a row, indicates the longest time

The next line, the number of a plurality of shows selected items

Description / Tips

1<=m,v<=200,n<=100

There must ensure data plan.

If a variety of programs, as small as possible in front of the output program.


01 backpack comes with two restrictions

In addition also a little recursive techniques to program output

Have you selected when k let f [i] [j] [k] represents dp [i] [j] transfer

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
const int N=205;
int a[N],b[N],val[N];
int dp[N][N];
bool f[N][N][N];
void output(int i,int j,int k){
    if(i==1){
        if(f[i][j][k])printf("1 ");
        return;
    }
    if(f[i][j][k])output(i-1,j-a[i],k-b[i]);
    else output(i-1,j,k);
    if(f[i][j][k])printf("%d ",i);
}
signed main(){
    int m,v,n;
    cin>>m>>v>>n;
    for(int i=1;i<=n;i++)
    scanf("%lld%lld%lld",&a[i],&b[i],&val[i]);
    for(int i=1;i<=n;i++){
        for(int j=m;j>=a[i];j--)
        for(int k=v;k>=b[i];k--)
        if(dp[j][k]<dp[j-a[i]][k-b[i]]+val[i]){
            dp[j][k]=dp[j-a[i]][k-b[i]]+val[i];
            f[i][j][k]=1;
        }
    }
    cout<<dp[m][v]<<endl;
    output(n,m,v);
}

Guess you like

Origin www.cnblogs.com/naruto-mzx/p/11606956.html