Multiple backpacks-hdu2191, full backpack-hdu2138, two-dimensional cost backpack hdu2159

Source: Internet
Author: User
/* Multiple backpacks, which are converted into 01 backpacks using the binary splitting method */# include <cstdio> # include <cstring> # define maxn 99999 # define max (, b) (a)> (B )? (A) :( B) int DP [maxn], C [maxn], V [maxn]; int main () {int Z, V, M, I, A, B, x, J; scanf ("% d", & Z); While (Z --) {scanf ("% d", & V, & M ); int CNT = 1; for (I = 1; I <= m; I ++) {scanf ("% d", & A, & B, & X); int T = 1; while (x> = T) {v [CNT] = A * t; C [CNT ++] = B * t; x-= T; t <1;} If (x) {v [CNT] = A * X; C [CNT ++] = B * X ;}} memset (DP, 0, sizeof (DP); for (I = 1; I <CNT; I ++) for (j = V; j> = V [I]; J --) DP [J] = max (DP [J], DP [J-V [I] + C [I]); printf ("% d \ n ", DP [v]);} return 0 ;}

 

/* Completely converted from a backpack to a repeated 01 backpack */# include <cstdio> # include <cstring> # define max (a, B) (a)> (B )? (A) :( B) # define maxn 100010int DP [100010]; int main () {int N, I, j, V; int C [200], V [200]; while (scanf ("% d", & N )! = EOF) {memset (DP, 0, sizeof (DP); for (I = 1; I <= N; I ++) scanf ("% d ", & C [I], & V [I]); scanf ("% d", & V); for (I = 1; I <= N; I ++) for (j = V [I]; j <= V; j ++) DP [J] = max (DP [J], DP [J-V [I] + C [I]); printf ("% d \ n", DP [v]);} return 0 ;}

 

/* The two-dimensional cost is much deformed. The triple cycle is hard to grasp. */# Include <cstdio> # include <cstring> # define maxn 150 # define max (a, B) (a)> (B )? (A) :( B) int main () {int C [maxn], V [maxn], DP [maxn] [maxn]; int ex, Pa, KI, mm, i, J, K; while (scanf ("% d", & Ex, & PA, & Ki, & mm )! = EOF) // experience, patience, kinds, mostmonserts {int F = 1; memset (DP, 0, sizeof (DP); for (I = 1; I <= Ki; I ++) scanf ("% d", & C [I], & V [I]); for (I = 1; I <= Ki; I ++) for (j = V [I]; j <= PA; j ++) for (k = 1; k <= mm; k ++) DP [J] [k] = max (DP [J] [K], DP [J-V [I] [k-1] + C [I]); for (I = 1; I <= PA; I ++) for (k = 1; k <= mm; k ++) if (DP [I] [k]> = ex) {f = 0; printf ("% d \ n ", pa-I); goto a;} If (f) printf ("-1 \ n"); A:;} return 0 ;}

 

 

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.