码迷,mamicode.com
首页 > 其他好文 > 详细

TYVJ1276

时间:2014-08-24 23:46:53      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   art   

多重背包+二进制优化
这是怎么了,连做俩题都是这东西,不多说了,TYVJ1194
唯一的特点是当mi等于-1时,就 mi=g/gi;
剩下的就是普通的多重背包二进制优化了。

 

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxv = 50005;
 7 const int maxn = 500;
 8 int dp[maxv],w[maxn],v[maxn];
 9 int main()
10 {
11    freopen("in.txt","r",stdin);
12     int n,m,cnt = 0;
13     cin>>n>>m;
14     for(int i = 1;i<=n;++i)
15     {
16         int a,g,p;
17         cin>>a>>g>>p;
18         if(a==-1)a = m/g;
19         int x = 1;
20         while(a>=x)
21         {
22             w[cnt] = x*g;
23             v[cnt++] = x*p;
24             a-=x;
25             x*=2;
26         }
27         if(a){w[cnt] = a*g;v[cnt++]=a*p;}
28     }
29     printf("cnt=%d\n",cnt);
30     memset(dp,0,sizeof(dp));
31     for(int i = 0;i<cnt;++i)
32         for(int j = m;j>=w[i];--j)
33             dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
34     printf("%d\n",dp[m]);
35 
36     return 0;
37 }
View Code

 

TYVJ1276

标签:style   blog   http   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/GJKACAC/p/3933877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!