hdu2159 fate Two-dimensional full backpack

Source: Internet
Author: User

The typical two-dimensional knapsack problem, because the topic said each strange number of infinite//start at the time I want to define a three-dimensional dp,dp[i][j][k] represents the first I species//strange kill J only remaining endurance point for dp[i][j][k] The maximum value of experience, then state// The equation can be obtained as dp[i][j][k] = max (dp[i-1][j][k],dp[i][j-1][k-b[i]]+a[i]);//This and the complete knapsack equation expatiating, only the final answer I am not very clear how to find the big,//finally looked at the solution, My idea was right, but the final result would be to find the biggest j that meets the conditions. The book on the design of a complete backpack space savings, less to one-dimensional//dp[j][k] for the durability point of J, but also to kill the odd number k when the maximum value of experience//is dp[ J][k] = max (dp[j][k],dp[j-b[i]][k-1]+a[i); #include <algorithm> #include <bitset> #include <cassert > #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include < complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define CEIL ( A, B) ((((a) + (b)-1)/(b)) #define Endl ' \ n ' #define GCD __gcd#define highbit (x) (1ull<< (63-__builtin_clzll (x))) #Define Popcount __builtin_popcountlltypedef Long long ll;using namespace Std;const int MOD = 1000000007;const long double PI = ACOs ( -1.L); Template<class t> inline T LCM (const t& A, const t& b) {return A/GCD (A, b) *b;} Template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} const int MAXN = 108;int a[maxn];int b[maxn];int dp[maxn][maxn];int n,m,k,s;void init () {for (int i=1;i<=k;i++) scanf ("% D%d ", &a[i],&b[i]); Memset (Dp,0,sizeof (DP));} void print () {for (int. i=1;i<=m;i++) {for (int j=1;j<=s;j++) printf ("%d", Dp[i][j]);p UTS ("");}} void Solve () {for (int i=1;i<=k;i++) {for (int j=b[i];j<=m;j++)//This heavy loop is a complete backpack and can only go over for (int l=s;l>=1;l--)// This heavy loop can come along dp[j][l] = max (Dp[j][l],dp[j-b[i]][l-1]+a[i]);} int flag = 0;int cnt=0;for (int i=m;i>=0;i--) for (int j=1;j<=s;J + +) if (dp[i][j]>=n) {flag=1;cnt=i;break;} if (!flag) printf (" -1\n"); else{printf ("%d\n", m-cnt);} Print ();} int main () {//freopen ("G:\\code\\1.txt", "R", stdin), while (scanf ("%d%d%d%d", &n,&m,&k,&s)!=eof) { Init (); Solve ();} return 0;}

hdu2159 fate Two-dimensional full backpack

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.