[Backpack TEMPLATE]

Source: Internet
Author: User
Int V; // capacity // 01 void zeroone (INT cost, int Val) {// fee, value int I; for (I = V; I >= cost; I --) {If (DP [I] <DP [I-cost] + val) {DP [I] = DP [I-cost] + val ;}}} // complete backpack void complete (INT cost, int Val) {// fee, value int I; for (I = cost; I <= V; I ++) {If (DP [I] <DP [I-cost] + val) {DP [I] = DP [I-cost] + val ;}}} // multiple backpacks void multi (INT cost, int Val, int CNT) {// fee, value, quantity if (Cost * CNT> = V) {complete (cost, val); return;} int K = 1; while (k <CNT) {zeroone (K * cost, K * val); CNT-= K; K * = 2 ;} zeroone (CNT * cost, CNT * val);} // three backpack types: for (I = 0; I <m; I ++) {scanf ("% d", & V, & weight, & Value, & N); If (V> v | weight> W) continue; if (n = 1) zeroone (v, weight, value); else if (n = 0) complete (v, weight, value); else multiply (v, weight, value, n);} // two-dimensional backpack: void zeroone (int v, int weight, int value) {int I, j; for (I = V; i> = V; I --) for (j = W; j> = weight; j --) if (DP [I] [J] <DP [I-v] [J-weight] + value) DP [I] [J] = DP [I-v] [J-weight] + value;} void complete (int v, int weight, int value) {int I, j; for (I = V; I <= V; I ++) for (j = weight; j <= W; j ++) if (DP [I] [J] <DP [I-v] [J-weight] + value) DP [I] [J] = DP [I-v] [J-weight] + value;} void multiply (INT V, int weight, int value, int count) {If (V * count> = v | weight * count> = W) {complete (v, weight, value); return;} int K = 1; while (k <count) {zeroone (K * v, k * weight, K * value); count-= K; K * = 2;} zeroone (count * V, count * weight, count * value);} // grouping backpack: for (I = 1; I <= N; I ++) {// enumeration group for (j = m; j> = 1; j --) {// backpack capacity for (k = 1; k <= m; k ++) {// items with different charges if (j> = k) // The transfer equation has J-K, ensure that J-k> = 0 DP [J] = max (DP [J], DP [J-K] + A [I] [k]);}

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.