01背包和背包完全

RT。职务地址http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=2123

ps:(1)01背包和全然背包的差别在于计算方向,01背包的计算顺序是从上往下,从右往左;全然背包则是从上往下,从左往右

(2)背包是否装满的差别在于初始化第0行,若不一定装满则所有初始化为0,

若要求必须装满则dp[0]=0,dp[1~C]=(求最小值时初始化为INF,求最大值时初始化为-INF)

(3)终于返回的dp[C]即为结果。

python代码:

#coding=utf-8
INF = 1000000
def Matrix(rows,cols):
    #matrix = [[0 for col in range(cols)] for row in range(rows)]
    matrix = [0 for col in range(cols)]
    return matrix

t = input()
while t:
    [x,y]=raw_input().split()
    C=int(y)-int(x)
    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 range(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

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-08-26 11:39:09

01背包和背包完全的相关文章

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

01背包&完全背包

·01背包&完全背包基础 01背包模型:给定n个物品,第i个物品体积为Wi,价值为Vi,背包容量为sum,选择一些物品放入背包,要求总价值最大. F[i,j]表示前i个物品放入容量为j的包里获得的最大价值. 对于任意一个物品都有两种状态,要么放要么不放,不放的话很显然价值同前,放的话就要从包里拿出一部分体积. 完全背包模型:给定n种物品,第i个物品体积为Wi,价值为Vi,背包容量为sum,选择一些物品放入背包,要求总价值最大. F[i,j]表示前i种物品放入容量为j的包里获得的最大价值. 01背

HDU2191_悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(背包/多重背包)

解题报告 题目传送门 题意: 中文不多说; 思路: 基础多重背包,每个物品有多个可以选,转换成01背包解. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int main() { int t,i,j,n,m,v,p,h,cc,w[1010],c[1010],dp[1010]; scanf("%d",&

POJ3260——The Fewest Coins(多重背包+完全背包)

The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus

ZOJ 3164 Cookie Choice 分组背包 混合背包

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3181 题意: 就是混合背包加分组背包,有的物品是01背包,有的是多重背包,有的是完全背包,同时物品还有不超过8组的分组,如果在同一组则最多只能选一种.问能不能恰好地用掉D的容量,并且使所获价值最大. 分析: 开始的想法是多开一个下标,先把没有分组的做了,在0的下标,然后分组分别在组号的下标里按顺序处理,是什么背包就用什么做法,不过一直WA,对拍小数据大数据都没啥问题(可能随机

HDU1114_Piggy-Bank(背包/完全背包)

解题报告 题目传送门 题意: 给金币的面额和重量,求装满储蓄罐的最小价值. 思路: 完全背包基础,初始dp为最大,dp[0]=0表示储蓄罐为空价值为0; 状态转移方程就是dp[j]=min(dp[j],dp[j-w[i]]+c[i]) #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int main() { int t,i

POJ 3260 多重背包+完全背包

前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168.1.1就能看到他的路由器的一切信息,包括密码,然后打开笔记本……好了,废话不多说,能连上网后第一时间当然是继续和队友之前约好的训练了. 今天翻看到之前落下的一道混合背包题目,然后在草稿本上慢慢地写递推方程,把一些细节细心地写好…(本来不用太费时间的,可是在家嘛,一会儿妈走来要我教她玩手机,一会儿有一个亲戚朋

HDU1248_寒冰王座(背包/完全背包)

解题报告 题目传送门 无聊的水题. #include <iostream> #include <cstring> #include <cstdio> #define inf 0x3f3f3f3f using namespace std; int c[10100],w[10100],dp[11000],v,n; int main() { int t,i,j; scanf("%d",&t); while(t--) { memset(dp,0,si

B 二维背包+完全背包 Hdu2159

<span style="color:#3333ff;">/* ------------------------------------------------------------------------------------------------ author : Grant Yuan time : 2014.7.19 aldorithm: 二维背包+完全背包 ----------------------------------------------------

B 维背包+完全背包 Hdu2159

<span style="color:#3333ff;">/* ------------------------------------------------------------------------------------------------ author : Grant Yuan time : 2014.7.19 aldorithm: 二维背包+全然背包 ----------------------------------------------------