Backpack --HDU-2159-D

Title meaning

 Ok. . . Each monster has a corresponding degree of experience and patience

Q. Can you get the experience needed to upgrade in a limited number of endurance and killing

If so, enter the maximum degree of tolerance level can be reserved Seung

Topic analysis

If there is no limit to the number killing is seen as a backpack the endurance capacity, look for the biggest backpack full experience of endurance can get m

If killing is a limit to the number of each monster, and that is a multiple backpack

But in the end neither is this question, but a number of two-dimensional backpack killing record

Topic Code

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=107;
int n,m,k,s;
struct node{
    int val;
    int cost;
}mon[507];
int dp[maxn][maxn];
int main(){
    while(~scanf("%d%d%d%d",&n,&m,&k,&s)){
        for(int i=1;i<=k;i++)
            scanf("%d%d",&mon[i].val,&mon[i].cost);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=k;i++)
        for(int j=mon[i].cost;j<=m;j++)
        for(int t=1;t<=s;t++){
            dp[j][t]=max(dp[j][t],dp[j-mon[i].cost][t-1]+mon[i].val);
        }
        if(dp[m][s]<n)printf("-1\n");
        else{
            int mx;
            for(mx=0;mx<=m;mx++)
                if(dp[mx][s]>=n)break;
            printf("%d\n",m-mx);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/helman/p/11232237.html