HDU2546 01背包
来源:互联网 发布:供销大数据集团怎么样 编辑:程序博客网 时间:2024/06/03 15:56
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546
解题思路:要使得买饭后卡上的余额达到最小,但由于有限制条件就是卡上的必须剩下不小于5元才能买饭,这样的情况用01背包是肯定错误的得不到最优的解,我们知道要使得卡上余额最小那就是要让最贵的菜最后买,所以剩下来的n-1种菜就可以用01背包求解了
#include<iostream>using namespace std;#define max(a,b) a>b?a:bint w[1005];int f[10005];int ZeroOneBack(int w[],int n,int momey,int p){for(int i=1;i<=n;i++){if(i!=p){for(int j=momey;j>=w[i];j--){f[j]=max(f[j],f[j-w[i]]+w[i]);}}}return f[momey];}int main(){int i,p,max;int m,n;while(cin>>n,n){max=0;memset(f,0,sizeof(f));for(i=1;i<=n;i++)cin>>w[i];cin>>m;for(i=1;i<=n;i++){if(w[i]>max){p=i;max=w[i];}}if(m<5)cout<<m<<endl;else{int s=ZeroOneBack(w,n,m-5,p);cout<<m-s-max<<endl;}}return 0;}
- HDU2546 01背包
- HDU2546:饭卡(01背包)
- hdu2546 饭卡 (01背包)
- hdu2546饭卡-01背包
- hdu2546饭卡 (01背包)
- HDU2546:饭卡(01背包)
- HDU2546 饭卡 01背包
- hdu2546 01背包
- HDU2546 (01背包)
- HDU2546 饭卡(01背包)
- HDU2546 01背包
- 【HDU2546】饭卡(01背包)
- 【01背包】HDU2546饭卡
- HDU2546(01背包)
- hdu2546饭卡(01背包)
- hdu2546-01背包
- hdu2546 饭卡(01背包)
- 01背包 hdu2546 hdu2602
- 学习使用SQLite(版本3.7.4)(上A)
- Firefox OS FOR i9250 build120731
- 杀掉D状态的进程
- mysql字符集操作
- struts标签库:logic:empty logic:notPresent 界面
- HDU2546 01背包
- TUS,真正的免费在线Linux云电脑
- struts标签库:logic:empty logic:notPresent Action
- 使用genstrings和NSLocalizedString实现App文本的本地化
- struts标签库:logic:empty logic:notPresent web.xml
- CGRect的一些用法
- Java监控文件变化
- struts标签库:logic:empty logic:notPresent struts-config.xml
- QEMU Emulator User Documentation