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