POJ 2923 Relocation (状态压缩 0-1背包)
来源:互联网 发布:淘宝上哪家皮草店好 编辑:程序博客网 时间:2024/05/22 08:02
题目链接:(—_—) zZ
题目大意:有n个物品用2个车来运输, 每个物品有一定的重量, 每个车能承受一定的重量, 现在要求用车把物品从老房子运输到新房子, 问至少要运多少趟
Ps:我太弱了, 不会状态压缩, 第一回乱搞超时,看了大牛的状态压缩,哦了
code:
#include <stdio.h>#include <string.h>#define min(a, b) a>b? b:aint n =0, c[2], w[12], s[1026], dp[1026];int judge(int x){ int i = 0, j = 0, sum = 0, dp[1026]; memset(dp, 0, sizeof(dp)); dp[0] = 1; for(i = 0; i<n; i++) { if(x&(1<<i)) { sum += w[i]; for(j = c[0]; j>=w[i]; j--) if(dp[j-w[i]]) dp[j] = 1; } } for(i = 0; i<=c[0]; i++) if(dp[i] && sum-i<=c[1]) return 1; return 0;}int main(){ int i = 0, j = 0, t = 0, item = 0, count = 0, count1 = 0; scanf("%d", &t); while(t--) { count =0; scanf("%d %d %d", &n, &c[0], &c[1]); item = 1<<n; for(i = 0; i<n; i++) scanf("%d", &w[i]); for(i = 0; i<item; i++) if(judge(i)) s[count++] = i;//判断当前方案能否运输一趟 for(i = 0; i<item; i++) dp[i] = 1234567890; dp[0] = 0; for(i = 0; i<count; i++) { for(j = item-1-s[i]; j>=0; j--) if(!(j&s[i]))//每个物品只能运输一趟, 不能重复 dp[j|s[i]] = min(dp[j|s[i]], dp[j]+1);//在运输了状态j上,在运输s[i], 变成状态j|s[i] } printf("Scenario #%d:\n", ++count1); printf("%d\n\n", dp[item-1]); } return 0;}
- POJ 2923 Relocation (状态压缩 0-1背包)
- POJ 2923 Relocation 状态压缩DP + 0-1背包
- 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(状态压缩+ 01背包)
- POJ 2923 Relocation (状态压缩+背包)
- POJ 2923 Relocation(状态压缩+背包思想)
- poj 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation(DP+状态压缩+01背包)
- poj 2923 Relocation(状态压缩dp+背包)
- poj 2923 Relocation(状态压缩+01背包)
- POJ 2923 Relocation(状态压缩+01背包)
- poj 2923 Relocation (01背包+状态压缩)
- POJ 2923 Relocation(状态压缩DP+DP:01背包)
- camera2
- MongoDB设置访问权限、设置用户
- oracle表空间
- Spring jdbc 批量更新
- camera3
- POJ 2923 Relocation (状态压缩 0-1背包)
- android 各种控件颜色值的设置(使用Drawable,Color)
- camera4
- JBOSS多实例部署出现Table already exists: JMS_MESSAGES in statement[更新]
- HDU_4317 Unfair Nim 状态压缩dp
- camera5
- 思维、找规律题目汇总
- ubuntu bluetooth 配对过程
- 2012-7-31 总结