http://acm.hdu.edu.cn/showproblem.php?pid=3591&&背包问题
2012-03-20 20:47
567 查看
多重和完全背包组成的混合背包。。构造dp[i]为付i元钱需要的最少的钱的张数,,,然后二进制思想转化为0-1背包。。。
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[200010];
int v[1400];
int w[1400];
int a[101];
int main()
{
int n,T;
int res=0;
while(cin>>n>>T)
{
if(n==0&&T==0)break;
int cnt=0;
for(int i=1;i<=20000;++i)
dp[i]=0x3f7f7f7f;
dp[0]=0;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i)
{
int k;
cin>>k;
int t=1;
while(2*t<k+1)
{
w[cnt]=t*a[i];
v[cnt++]=t;
t=2*t;
}
w[cnt]=(k+1-t)*a[i];
v[cnt++]=k+1-t;
}
for(int i=0;i<cnt;++i)
{
if(w[i]>20000) continue;
for(int j=20000;j>=w[i];--j)
dp[j]=min(dp[j-w[i]]+v[i],dp[j]);
}
int ans=0x3f7f7f7f;
for(int i=T;i<=20000;++i)
if(dp[i]+dp[i-T]<ans) ans=dp[i]+dp[i-T];
if(ans==0x3f7f7f7f) cout<<"Case "<<++res<<": -1"<<endl;
else cout<<"Case "<<++res<<": "<<ans<<endl;
}return 0;
}
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[200010];
int v[1400];
int w[1400];
int a[101];
int main()
{
int n,T;
int res=0;
while(cin>>n>>T)
{
if(n==0&&T==0)break;
int cnt=0;
for(int i=1;i<=20000;++i)
dp[i]=0x3f7f7f7f;
dp[0]=0;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i)
{
int k;
cin>>k;
int t=1;
while(2*t<k+1)
{
w[cnt]=t*a[i];
v[cnt++]=t;
t=2*t;
}
w[cnt]=(k+1-t)*a[i];
v[cnt++]=k+1-t;
}
for(int i=0;i<cnt;++i)
{
if(w[i]>20000) continue;
for(int j=20000;j>=w[i];--j)
dp[j]=min(dp[j-w[i]]+v[i],dp[j]);
}
int ans=0x3f7f7f7f;
for(int i=T;i<=20000;++i)
if(dp[i]+dp[i-T]<ans) ans=dp[i]+dp[i-T];
if(ans==0x3f7f7f7f) cout<<"Case "<<++res<<": -1"<<endl;
else cout<<"Case "<<++res<<": "<<ans<<endl;
}return 0;
}
相关文章推荐
- http://acm.hdu.edu.cn/showproblem.php?pid=2844&&背包问题
- http://acm.hdu.edu.cn/showproblem.php?pid=2191&&多重背包+二进制优化
- http://acm.hdu.edu.cn/showproblem.php?pid=2159&&二维完全背包
- http://acm.hdu.edu.cn/showproblem.php?pid=3790&&最短路径问题
- http://acm.hdu.edu.cn/showproblem.php?pid=1248&&完全背包
- http://acm.hdu.edu.cn/showproblem.php?pid=1114&&完全背包
- http://acm.hdu.edu.cn/showproblem.php?pid=1171&&0-1&&多重背包混合
- 矩阵问题&&http://acm.hdu.edu.cn/showproblem.php?pid=1575
- http://acm.hdu.edu.cn/showproblem.php?pid=1059&&多重背包
- http://acm.hdu.edu.cn/showproblem.php?pid=1203(背包问题)
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?&&次小生成树
- Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312
- http://acm.hdu.edu.cn/showproblem.php?pid=2795&&线段树之求最小区间端点值
- Holedox Eating &&http://acm.hdu.edu.cn/showproblem.php?pid=4302
- http://acm.hdu.edu.cn/showproblem.php?pid=1023&&卡特兰数
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- http://acm.hdu.edu.cn/showproblem.php?pid=2680&&Choose the best route
- 完数 &&http://acm.hdu.edu.cn/showproblem.php?pid=1406
- http://acm.hdu.edu.cn/showproblem.php?pid=1394&&线段树之求逆序数
- http://acm.hdu.edu.cn/showproblem.php?pid=4337&&dfs