poj2392 多重背包

 1 //Accepted    868 KB    188 ms
 2 //多重背包
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <queue>
 7 #include <cmath>
 8 #include <algorithm>
 9 using namespace std;
10 /**
11   * This is a documentation comment block
12   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
13   * @authr songt
14   */
15 const int imax_v = 40005;
16 const int imax_n = 405;
17 int dp[imax_v];
18 int n;
19 struct node
20 {
21     int weight,c,a;
22 }f[imax_n];
23 //按a排序,考虑我们在dp时采用的是倒推,所以我们需要先计算出a较小的时候的情况
24 int cmp(node x,node y)
25 {
26     return x.a<y.a;
27 }
28 int max(int a,int b)
29 {
30     return a>b?a:b;
31 }
32 void zeroOnePack(int weight,int value,int v)
33 {
34     for (int j=v;j>=weight;j--)
35     {
36         dp[j]=max(dp[j],dp[j-weight]+value);
37     }
38 }
39 void completePack(int weight,int value,int v)
40 {
41     for (int j=weight;j<=v;j++)
42     {
43         dp[j]=max(dp[j],dp[j-weight]+value);
44     }
45 }
46 void multiplePack(int weight,int value,int amount,int v)
47 {
48     int k=1;
49     if (amount*weight>=v)
50     {
51         completePack(weight,value,v);
52         return ;
53     }
54     while (k<amount)
55     {
56         zeroOnePack(k*weight,k*value,v);
57         amount-=k;
58         k<<=1;
59     }
60     zeroOnePack(amount*weight,amount*value,v);
61 }
62 void Dp()
63 {
64     memset(dp,0,sizeof(dp));
65     for (int i=1;i<=n;i++)
66     {
67         multiplePack(f[i].weight,f[i].weight,f[i].c,f[i].a);
68     }
69     int ans=0;
70     for (int i=1;i<=40000;i++)
71     ans=max(ans,dp[i]);
72     printf("%d\n",ans);
73 }
74 int main()
75 {
76     while (scanf("%d",&n)!=EOF)
77     {
78         for (int i=1;i<=n;i++)
79         scanf("%d%d%d",&f[i].weight,&f[i].a,&f[i].c);
80         sort(f+1,f+n+1,cmp);
81         Dp();
82     }
83     return 0;
84 }

时间: 2024-10-03 22:25:22

poj2392 多重背包的相关文章

[hdu5445 Food Problem]多重背包

题意:一堆食物,有价值.空间.数量三种属性,一些卡车,有空间,价格,数量三种属性.求最少的钱(不超过50000)买卡车装下价值大于等于给定价值的食物,食物可以拆开来放. 思路:这题的关键是给定的条件:食物可以拆开来放.这个条件使得卡车和食物可以分开考虑,然后通过空间这个属性联系在一起.做两遍多重背包即可. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

hdu 2079 选课时间(题目已修改,注意读题) 多重背包

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3162    Accepted Submission(s): 2472 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别

HDU-1171 Big Event in HDU (多重背包)

Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.The splitting is absolutely a big

Holding Bin-Laden Captive!(1.多重背包 2.母函数)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41 Accepted Submission(s): 29   Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disap

HDU1171--Big Event in HDU(多重背包)

Big Event in HDU   Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1139 Accepted Submission(s): 444 Problem Description Nowadays, we all know that Computer College is the biggest department in HD

HDU5677 manacher + 二维多重背包

附上题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5677 问题描述 ztr喜欢研究子串,今天,他有n个串 现在ztr想知道,能否从这n个串的所有回文子串中, 取出恰好k个回文串且满足这些回文串的长度之和为L 以yjqqaq为例 这个串包含的回文子串有 y,j,q,a,q,qq,qaq 所以我们可以既选qq,又选qaq 输入描述 有T组数据,第一行为一个正整数T(T<=10)T(T<=10) 每组数据第一行为三个正整数N(1<=N<

HDU 2191 多重背包

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22865    Accepted Submission(s): 9661 Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

hdu 1059 (多重背包) Dividing

这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的两份. 联想到多重背包,稍微用二进制优化一下.(最近身体不适,压力山大啊) 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define inf 70000 5 using namespace s