*space elevator<pku 2392>

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 // date : 5.4
2 // problem : dp
3 #include < iostream >
4 #include < cstdio >
5 #include < algorithm >
6 using namespace std;
7 struct node
8 {
9 int cost,w,nk;
10 }d[ 405 ];
11 int f[ 40005 ];
12 bool cmp(node a,node b)
13 {
14 return a.w <= b.w;
15 }
16 /// 模板
17 void one( int cost, int w, int m) // 完全背包
18 {
19 int i;
20 for (i = cost;i <= m;i ++ )
21 {
22 f[i] = max(f[i],f[i - cost] + w);
23 }
24 }
25
26 void one_zero( int cost, int w, int m) // 0/1背包
27 {
28 int i;
29 for (i = m;i >= cost;i -- )
30 {
31 f[i] = max(f[i],f[i - cost] + w);
32 }
33 }
34 void solve( int cost, int w, int nk)
35 {
36 if (cost * nk >= w) one(cost,cost,w); // 完全背包..
37 else // 不大于总重量的情况,就是 0/1 背包
38 {
39 int k = 1 ;
40 while (k < nk) // 把每件物品的个数转化成二进制
41 {
42 one_zero(k * cost,k * cost,w);
43 nk -= k;
44 k *= 2 ;
45 }
46 one_zero(nk * cost,nk * cost,w);
47 }
48 return ;
49 }
50 int main()
51 {
52 int n;
53 cin >> n;
54 int maxx =- 1 ;
55 for ( int i = 0 ;i < n;i ++ )
56 {
57 cin >> d[i].cost >> d[i].w >> d[i].nk;
58 maxx = max(maxx,d[i].w);
59 }
60 sort(d,d + n,cmp); // weight
61 for ( int i = 0 ;i < n;i ++ )
62 {
63 // int cost,w,nk;
64 // scanf("%d %d %d",&cost,&w,&nk); // 价值.重量.个数.
65 solve(d[i].cost,d[i].w,d[i].nk);
66 }
67 int maxxx = f[ 0 ];
68 for ( int i = 1 ;i <= maxx;i ++ )
69 {
70 if (f[i] > maxxx)
71 maxxx = f[i];
72 }
73 cout << maxxx << endl;
74 return 0 ;
75 }

转载于:https://www.cnblogs.com/eth0/archive/2011/05/04/2036230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值