POJ 2923 Relocation(状态压缩+背包思想)
来源:互联网 发布:2015年外贸出口数据 编辑:程序博客网 时间:2024/05/01 20:55
题目分析
因为是判断2辆车能够运多少次才能把所有的物品给运完,因为物品数不够多,因此可以枚举状态,判断每个状态能否由2辆车给运完,这里面可以用01背包的思想进行判断,然后自己就可以通过状态压缩写出状态转移方程,很明显
dp[(1<<n)−1] 就是答案。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n, C1, C2;const int maxn = 1<<12;const int INF = 0x3f3f3f3f;int a[12], ok[maxn], dp[maxn];bool judge(int state){ int val[105]; memset(val, 0, sizeof(val)); val[0] = 1; int sum = 0, sum1 = 0; for(int i = 0; i < n; i++){ if(state & (1<<i)){ sum += a[i]; for(int j = C1; j >= 0; j--) if(j - a[i] >= 0 && val[j-a[i]]) val[j] = 1; } } for(int j = C1; j >= 0; j--) if(val[j]) {sum1 = j; break;} if(sum - sum1 > C2) return false; return true;}int main(){ int T; scanf("%d", &T); for(int kase = 1; kase <= T; kase++){ scanf("%d%d%d", &n, &C1, &C2); for(int i = 0; i < n; i++) scanf("%d", &a[i]); int cnt = 0; for(int i = 0; i < (1<<n); i++){ dp[i] = INF; if(judge(i)) ok[cnt++] = i; } dp[0] = 0; for(int i = 0; i < (1<<n); i++) for(int j = 0; j < cnt; j++) dp[i|ok[j]] = min(dp[i|ok[j]], dp[i]+1); printf("Scenario #%d:\n%d\n\n", kase, dp[(1<<n)-1]); } return 0;}
0 0
- POJ 2923 Relocation(状态压缩+背包思想)
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation 状态压缩01背包
- poj 2923 Relocation 状态压缩+01背包
- POJ 2923 - Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+ 01背包)
- POJ 2923 Relocation (状态压缩+背包)
- poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)
- POJ 2923 Relocation (状态压缩 0-1背包)
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation(DP+状态压缩+01背包)
- poj 2923 Relocation(状态压缩dp+背包)
- POJ 2923 Relocation 状态压缩DP + 0-1背包
- POJ 2923 Relocation(状态压缩DP+DP:01背包)
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation(状态压缩加上01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- 分析Java线程池执行原理
- 伪类after before如何使用阿里的iconfont字体
- js 图片尺寸与格式验证
- CSS 元素垂直居中的 6种方法
- 【51nod 算法马拉松19 C】 集合对
- POJ 2923 Relocation(状态压缩+背包思想)
- 如何完全删除MySQL数据库,实现重装
- struts里面值栈valuestack和上下文contextMap的理解和使用
- 大神手把手教你Java性能优化-江南白衣(加强版)
- Android app 优化
- (2)行列式的性质
- 排序之 快排
- 开发之保存用户的登陆密码
- 数据结构之顺序表3(顺序表的创建、插入、删除········)