http://acm.hdu.edu.cn/showproblem.php?pid=1114&&完全背包
2012-03-20 11:13
253 查看
题意:硬币的数量不限制,让储蓄罐装满时得到的最少价值。
思路:先将完全背包转化成多重背包,再转化成0-1背包来解,不过这里要进行二进制优化,因为这里要保证储蓄罐要装满,所以需要先把容量为0时,价值赋为0,因为让求最小价值,因此需要把大于0的容量都赋为无穷大,如果让求的是装满时的最大价值则要把大于0的容量都赋为无穷小。。。
完全背包:
#include<iostream>
#include<string.h>
#include<algorithm>
#define N 10001
using namespace std;
int dp
;
int w[501],v[501];
int main()
{
int t;
cin>>t;
while(t--)
{
int p,q;
cin>>p>>q;
p=q-p;
int n;
cin>>n;
for(int i=0;i<N;++i)
dp[i]=0xfffff;
dp[0]=0;
for(int i=0;i!=n;++i)
cin>>v[i]>>w[i];
for(int i=0;i<n;++i)
{
if(w[i]>p) continue;
for(int j=w[i];j<=p;++j)
{
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[p]<0xfffff) cout<<"The minimum amount of money in the piggy-bank is "<<dp[p]<<"."<<endl;
else cout<<"This is impossible."<<endl;
}return 0;
}
思路:先将完全背包转化成多重背包,再转化成0-1背包来解,不过这里要进行二进制优化,因为这里要保证储蓄罐要装满,所以需要先把容量为0时,价值赋为0,因为让求最小价值,因此需要把大于0的容量都赋为无穷大,如果让求的是装满时的最大价值则要把大于0的容量都赋为无穷小。。。
#include<iostream> #include<string.h> #include<algorithm> using namespace std; #define N 10000; int dp[10001]; int w[350005]; int v[350005]; int main() { int t; cin>>t; while(t--) { int p,q; cin>>p>>q; p=q-p; for(int i=0;i<10001;++i) dp[i]=0xfffff; dp[0]=0; int n; cin>>n; int tot=0; for(int i=0;i!=n;++i) { int a,b; cin>>a>>b; int k=p/b; int t=1; while(2*t<k+1) { w[tot]=t*b; v[tot++]=t*a; t=2*t; } w[tot]=(k+1-t)*b; v[tot++]=(k+1-t)*a; } for(int i=0;i<tot;++i) { if(w[i]>p) continue; for(int j=p;j>=w[i];--j) dp[j]=min(dp[j-w[i]]+v[i],dp[j]); } if(dp[p]<0xfffff) cout<<"The minimum amount of money in the piggy-bank is "<<dp[p]<<"."<<endl; else cout<<"This is impossible."<<endl; }return 0; }
完全背包:
#include<iostream>
#include<string.h>
#include<algorithm>
#define N 10001
using namespace std;
int dp
;
int w[501],v[501];
int main()
{
int t;
cin>>t;
while(t--)
{
int p,q;
cin>>p>>q;
p=q-p;
int n;
cin>>n;
for(int i=0;i<N;++i)
dp[i]=0xfffff;
dp[0]=0;
for(int i=0;i!=n;++i)
cin>>v[i]>>w[i];
for(int i=0;i<n;++i)
{
if(w[i]>p) continue;
for(int j=w[i];j<=p;++j)
{
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[p]<0xfffff) cout<<"The minimum amount of money in the piggy-bank is "<<dp[p]<<"."<<endl;
else cout<<"This is impossible."<<endl;
}return 0;
}
相关文章推荐
- http://acm.hdu.edu.cn/showproblem.php?pid=2159&&二维完全背包
- http://acm.hdu.edu.cn/showproblem.php?pid=1248&&完全背包
- http://acm.hdu.edu.cn/showproblem.php?pid=1059&&多重背包
- http://acm.hdu.edu.cn/showproblem.php?pid=2191&&多重背包+二进制优化
- http://acm.hdu.edu.cn/showproblem.php?pid=3591&&背包问题
- http://acm.hdu.edu.cn/showproblem.php?pid=1171&&0-1&&多重背包混合
- http://acm.hdu.edu.cn/showproblem.php?pid=2844&&背包问题
- 最大连续子序列&&http://acm.hdu.edu.cn/showproblem.php?pid=1231
- http://acm.hdu.edu.cn/showproblem.php?pid=2852&&树状数组+二分
- 二分查找求函数的区间最小值&&http://acm.hdu.edu.cn/showproblem.php?pid=2899
- http://acm.hdu.edu.cn/showproblem.php?pid=4337&&dfs
- http://acm.hdu.edu.cn/showproblem.php?pid=3549&&ISAP
- Saving Princess claire_&&http://acm.hdu.edu.cn/showproblem.php?pid=4308
- http://acm.hdu.edu.cn/showproblem.php?pid=2066&&多源多点
- http://acm.hdu.edu.cn/showproblem.php?pid=1055&&Color a Tree
- 2^x mod n = 1&&http://acm.hdu.edu.cn/showproblem.php?pid=1395
- http://acm.hdu.edu.cn/showproblem.php?pid=2492&&求长度为3的顺序序列有多少个
- http://acm.hdu.edu.cn/showproblem.php?pid=1269&&迷宫城堡
- http://acm.hdu.edu.cn/showproblem.php?pid=3342&&拓扑入门题
- Clairewd’s message&&http://acm.hdu.edu.cn/showproblem.php?pid=4300