hdu3448(dfs解二维背包)
来源:互联网 发布:超级大傻瓜软件 编辑:程序博客网 时间:2024/04/30 00:50
链接:点击打开链接
题意:有k个物品,背包容量是m,问最多装n个物品最大的重量(k<=40,n<=40,剩余所有变量的范围是1~1000000000)
代码:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int n,k,w,ans,a[50];void dfs(int t,int num,int sum){ ans=max(ans,sum); if(num==k+1) return; if(sum+a[num]<=w&&t+1<=n) dfs(t+1,num+1,sum+a[num]); dfs(t,num+1,sum);} //因为无法开三维数组int main(){ //并且物品个数很少,因此用搜索 int i,sum; while(scanf("%d%d",&n,&w)!=EOF){ sum=0; scanf("%d",&k); for(i=1;i<=k;i++) scanf("%d",&a[i]); sort(a+1,a+k+1,greater<int>()); //不加会超时 for(i=1;i<=n;i++) sum+=a[i]; if(sum<=w){ printf("%d\n",sum); continue; } ans=0; dfs(0,1,0); printf("%d\n",ans); } return 0;}
0 0
- hdu3448(dfs解二维背包)
- dfs 解二维背包
- hdu3448 Bag Problem 背包搜索技巧
- POJ 1948 (Triangular Pastures)二维背包 DP/DFS
- 二维背包
- 二维背包
- 二维背包
- 二维背包
- HDU2159_FATE(背包/二维背包)
- 监狱调整 POJ1636 Prison rearrangement 传递闭包FLoyd DFS 二维背包 动态规划DP
- 背包dp之二维背包
- hdu2159二维完全背包
- 二维费用背包
- HDU 3496 二维背包
- 二维背包问题
- poj 1948 二维背包
- hdu3496 二维费用背包
- hdu3496二维背包
- 223.[Leetcode]Rectangle Area
- 自定义控件-组合控件
- python属性
- java中从键盘读入数据
- window.onload=函数名()与window.onload=函数名的区别
- hdu3448(dfs解二维背包)
- [HDU 5288] OO's Sequence (序列DP)
- 电商之梳理java基础-类加载器---java基础
- 动画
- 策略模式
- codeforces 679 B
- C++ heap学习
- 基于mysql的hive安装配置(apache-hive-2.0.1-bin.tar.gz)
- 【GOF23设计模式】_组合模式_树状结构_杀毒软件架构_JUnite底层架构JAVA239