01 Backpack and Backpack complete

Source: Internet
Author: User

Rt. Job Address http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=2123

PS: (1) 01 The difference between a backpack and a complete backpack is the direction of calculation, 01 of the backpack is calculated from the top down, from the right to the left, the total backpack is from the top down, from left to right

(2) Whether the backpack is full of the difference between the initialization of the No. 0 line, if not necessarily full, all initialized to 0,

dp[0]=0,dp[1~c]= (when the minimum value is initialized to INF when the requirement must be full, initialize to-inf when the maximum value is obtained)

(3) The return of the dp[c] is the result.

Python code:

#coding =utf-8inf = 1000000def Matrix (rows,cols):    #matrix = [[0 for Col in Range (cols)] for row in range (rows)]    mat Rix = [0 for col in Range (cols)]    return matrixt = input () and while T:    [X,y]=raw_input (). Split ()    C=int (y)-int (x) C5/>lines=input ()    n = lines    weight = [] Price    = [] While    lines:        [A,b]=raw_input (). Split ()        Weight.append (int (b))        price.append (int (a))        lines-= 1        if lines==0:break    mat = Matrix (n+1,c+1)    #init row-0 in matrix    mat[0]=0 for    J in Range (1,c+1): Mat[j]=inf    #compute The matrix for I in    RA Nge (1,n+1): for        J in Range (weight[i-1],c+1):            mat[j]=min (mat[j],mat[j-weight[i-1]]+price[i-1])    if Mat[c]==inf:print "This is impossible."    Else:print "The minimum amount of money in the Piggy-bank is%d."% mat[c]    T-= 1    if T==0:break


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

01 Backpack and Backpack complete

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.