Algorithm: POJ 2642 the Brick Stops here (01 backpack)

Source: Internet
Author: User

The main effect of the topic:

Brass brick is composed of copper and zinc elements, each piece of brass brick 1000 grams, copper accounted for among the 1~999 grams. Copper occupies a different proportion of the brass bricks belong to different classes. There are n brass bricks, giving them the weight of copper and the price.

Now there are C customers, each can buy a different type of brass brick, the request to the MI a brass brick melt, the copper station ratio for each kg in the "Cmin,cmax" grams.

Ask each customer at least how much money to pay to meet the requirements?

Analysis:

f [I][j][k], in the former I brick, to buy J bricks, the weight is less than the case of K, the minimum cost.
State transfer: F[i][j][k] = min (F[i][j][k], f[i-1][j][k], f[i-1][j-1][k-cost[i]]+val [i])
But three-dimensional space consumption is too large, so to compress the space into two-dimensional
According to the backpack nine, in the recursive, into reverse order can be.

Code:

 #include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <cm  
ath> #define MP Make_pair #define SQ (x) ((x) * (x)) using namespace Std;  
    
typedef long long Int64;  
Const double PI = ACOs (-1.0);  
const int MAXN = 210;  
const int INF = 0X3F3F3F3F;  
int n, M, COPPER[MAXN], PRICE[MAXN];  
int M[MAXN], CMIN[MAXN], CMAX[MAXN];  
    
int f[22][20010]; int main () {while (~SCANF ("%d", &n)) {for (int i=0; i<n; ++i) scanf ("  
    
        %d%d ", &copper[i], &price[i]);  
        scanf ("%d", &m);  
        int max_m = 0, max_weight=0;  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &m[i], &cmin[i], &cmax[i]);  
            if (M[i] > Max_m) max_m=m[i];  
        if (Cmax[i]*m[i] > max_weight) max_weight = Cmax[i]*m[i];  
        } memset (f, INF, sizeof (f));  
        F[0][0] = 0; for (int i=0; i<n; ++i) {for (int j=max_m; j>=1;--j) {for (int v=max_weight; v>=copper[i];--v) {  
                F[j][v] = min (F[j][v], f[j-1][v-copper[i]]+price[i]);  
            for (int i=0; i<m; ++i) {int minx = INF; for (int j=cmin[i]*m[i]; j<=cmax[i]*m[i]; ++j) if (F[m[i]][j] < minx) Minx =  
            F[M[I]][J];  
            if (minx = = INF) puts ("impossible");  
        else printf ("%d\n", Minx);  
} return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.