Hdu1864_ maximum reimbursement amount (Backpack/01 backpack), HDU1864 backpack

Source: Internet
Author: User

Hdu1864_ maximum reimbursement amount (Backpack/01 backpack), HDU1864 backpack

Solution report

Question Portal

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define inf 99999999using namespace std;int v,w[35],d[4],dw1,sum,dp[31*1000*100];int main(){    double Q,dw;    int n,i,j,m,t;    char c;    while(~scanf("%lf %d",&Q,&n)) {        m=0;        v=(int)(Q*100);        memset(dp,0,sizeof(dp));        memset(w,0,sizeof(w));        if(!n)break;        for(i=1; i<=n; i++) {            scanf("%d",&t);            sum=0;            int f=0;            memset(d,0,sizeof(d));            for(j=1; j<=t; j++) {                scanf(" %c:%lf",&c,&dw);                dw1=(int)(dw*100);                if(c>='A'&&c<='C') {                    d[c-'A']+=dw1;                    sum+=dw1;                } else f=1;            }            if(!f&&sum<=100000&&d[0]<=60000&&d[1]<=60000&&d[2]<=60000) {                w[m++]=sum;            }        }        for(i=0; i<m; i++) {            for(j=v; j>=w[i]; j--) {                dp[j]=max(dp[j],dp[j-w[i]]+w[i]);            }        }        printf("%.2lf\n",dp[v]/100.0);    }}

Maximum reimbursement amount Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 16358 Accepted Submission (s): 4775


Problem Description an existing fund can be reimbursed for a certain amount of invoices. The types of invoices that can be reimbursed include books (Class A), stationery (Class B), and travel (Class C). The total amount of each invoice must not exceed 1000 RMB, the value of a single item cannot exceed 600 RMB. Please write a program to find out the maximum reimbursement amount that can be reimbursed and does not exceed the given quota in a pile of invoices.
 
The Input test Input contains several test cases. The first row of each test case contains two positive Q and N, where Q is the specified reimbursement quota and N (<= 30) is the invoice count. Followed by N rows of input, the format of each line is:
M Type_1: price_1 Type_2: price_2... Type_m: price_m
The positive integer m is the number of items opened on the invoice, and Type_ I and price_ I are the types and values of items I. The item type is represented by an uppercase letter. When N is 0, all input ends, and the corresponding results are not output.
 
Output outputs one line for each test case, that is, the maximum amount that can be reimbursed, accurate to 2 digits after the decimal point.
 
Sample Input
200.00 32 A:23.50 B:100.001 C:650.003 A:59.99 A:120.00 X:10.001200.00 22 B:600.00 A:400.001 C:200.501200.50 32 B:600.00 A:400.001 C:200.501 A:100.00100.00 0


01 backpack Problems

P01: 01 backpack Problems
Question
There are N items and a backpack with a capacity of V. The cost of the I-th item is c [I], and the value is w [I]. Solving which items are loaded into a backpack can maximize the total value.
Basic Ideas
This is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not.
Define the state with a subproblem: that is, f [I] [v] indicates the maximum value that a backpack with a capacity of v can obtain when the first I item is placed. The state transition equation is:
F [I] [v] = max {f [I-1] [v], f [I-1] [v-c [I] + w [I]}
This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of v, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "pre-i-1 items into the capacity of v backpack", the value is f [I-1] [v]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity v-c [I ", the greatest value that can be obtained at this time is f [I-1] [v-c [I] plus the value w [I] obtained by placing the I item.
Optimize space complexity
The time and space complexity of the preceding method are O (N * V). The time complexity cannot be optimized, but the space complexity can be optimized to O (V ).
First, consider how to implement the basic idea mentioned above. There must be a main loop I = 1 .. n, two-dimensional array f [I] [0 .. all values of V. Then, if only one array f [0 .. v]. can we ensure that f [I] [v] represents the state we defined after the end of the I-th loop? F [I] [v] is derived from two subproblems: f [I-1] [v] and f [I-1] [v-c [I, can we ensure that f [I] [v] is pushed (that is, when f [v] is pushed in the I Main Loop) can we get the values of f [I-1] [v] and f [I-1] [v-c [I? In fact, this requires that in each main loop we use v = V .. 0 in order. f [v], this ensures that f [v] f [v-c [I] saves the value of State f [I-1] [v-c [I. The pseudocode is as follows:
For I = 1. N
For v = V .. 0
F [v] = max {f [v], f [v-c [I] + w [I]};
F [v] = max {f [v], f [v-c [I]} is equivalent to our transfer equation f [I] [v] = max {f [I-1] [v], f [I-1] [v-c [I]}, because the current f [v-c [I] is equivalent to the original f [I-1] [v-c [I]. If we change the circular order of v from the forward order to the order, then f [I] [v] is deduced by f [I] [v-c [I, it is not consistent with the meaning of this question, but it is another important backpack problem P02 is the most simple solution, so it is necessary to learn to solve the problem of 01 backpack with only one-dimensional array.
In fact, the program that uses a one-dimensional array to solve the 01 backpack will be used multiple times later, so here we abstract a process to process an item in the 01 backpack, direct calls in future Code are not described.
ZeroOnePack indicates processing an item in a 01 backpack. The two parameters cost and weight indicate the cost and value of this item respectively.
Procedure ZeroOnePack (cost, weight)
For v = V .. cost
F [v] = max {f [v], f [v-cost] + weight}
Note that the processing in this process is different from the pseudo code given above. The preceding example program v = V .. 0 is used to show that every state in the program is solved according to the equation, avoiding unnecessary complexity of thinking. Now that the process has been abstracted as a black box, you can add optimization. Items whose cost is cost will not affect the status f [0 .. cost-1], which is obvious.
With this process, 01 ...... the remaining full text>

Pascal's 01 backpack Problem

For I: = 1 to w do if I <a [1]. w then f [1, I]: = 0 else f [1, I]: = a [I]. v;
In the end, a [1]. v is not a [I]. v.
It should be changed later
For I: = 2 to n do
For j: = 1 to w do
If j <a [I]. w then f [I, j]: = f [I-1, j]
Else
Begin
T: = f [I-1, j-a [I]. w] + a [I]. v;
If f [I-1, j] <t then f [I, j]: = t else f [I, j]: = f [I-1, j];
End;
The comparison between j and a [I]. w should be outside. Otherwise f [I-1, j-a [I]. w] may have j-a [I]. w <0 causes the array to cross-border ..

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.