syhd142  
日历
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
统计
  • 随笔 - 23
  • 文章 - 122
  • 评论 - 31
  • 引用 - 0

导航

常用链接

留言簿(2)

随笔档案(23)

文章分类(270)

文章档案(122)

我的豆瓣

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
多重背包,Cui tianyi大牛的背包九讲里面说的很详尽。
#include <stdio.h>
#include 
<string.h>

#define N 105
#define M 100005
#define MAX(a, b) (a > b ? a : b)

int a[N], c[N], f[M];

void Complete(int cost, int weight, int m)
{
    
for(int i = cost; i <= m; i++)
        f[i] 
= MAX(f[i], f[i - cost] + weight);
}

void Zero_One(int cost, int weight, int m)
{
    
for(int i = m; i >= cost; i--)
        f[i] 
= MAX(f[i], f[i - cost] + weight);
}

int main()
{
    
int n, m;
    
while(scanf("%d %d"&n, &m), n + m)
    {
        
for(int i = 0; i < n; i++)
            scanf(
"%d"&a[i]);
        
        
for(int i = 0; i < n; i++)
            scanf(
"%d"&c[i]);
        
        memset(f, 
0sizeof(f));
        
        
for(int i = 0; i < n; i++)
        {
            
int t = a[i] * c[i];
            
if(t > m) Complete(a[i], a[i], m);
            
else
            {
                
int k = 1
                
while(k < c[i])
                {
                    Zero_One(k 
* a[i], k * a[i], m);
                    c[i] 
-= k;
                    k 
*= 2;
                }
                Zero_One(c[i] 
* a[i], c[i] * a[i], m);
            }
        }
        
int ans = 0;
        
for(int i = 1; i <= m; i++)
        {
            
if(f[i] == i) ans++;
        }
        printf(
"%d\n", ans);
    }
    
return 0;
}
posted on 2010-07-26 10:35 Fucker 阅读(634) 评论(1)  编辑 收藏 引用 所属分类: ACM/ICPCDP
评论:
  • # re: HDU 2844 Coins[未登录]  无名 Posted @ 2012-09-13 21:18
    for(int i = 1; i <= m; i++)
    {
    if(f[i] == i) ans++;
    }
    大神这个怎么来的,我看不懂  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © Fucker Powered by: 博客园 模板提供:沪江博客