题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完。

思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走。然后状压DP运的顺序。

代码:

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1024 + 10;
const int M = maxn * 30;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int c1, c2, n;
int w[maxn];
int dp[1 << 12];
int can[maxn];
int bag[maxn], v[maxn];
int main(){
int T, ca = 1;
scanf("%d", &T);
while(T--){
scanf("%d%d%d", &n, &c1, &c2);
for(int i = 0; i < n; i++) scanf("%d", &w[i]); int cnt = 0;
int tot = 0, sum = 0;
for(int i = 1; i < (1 << n); i++){
tot = sum = 0;
for(int j = 0; j < n; j++){
if(i & (1 << j)) v[++tot] = w[j], sum += w[j];
}
memset(bag, 0, sizeof(bag));
for(int j = 1; j <= tot; j++){
for(int k = c1; k >= v[j]; k--){
bag[k] = max(bag[k], bag[k - v[j]] + v[j]);
}
}
if(c2 >= sum - bag[c1]) can[cnt++] = i;
} memset(dp, INF, sizeof(dp));
dp[0] = 0;
for(int i = 0; i < (1 << n); i++){
if(dp[i] == INF) continue;
for(int j = 0; j < cnt; j++){
if((i & can[j]) == 0){
dp[i | can[j]] = min(dp[i | can[j]], dp[i] + 1); }
}
} printf("Scenario #%d:\n%d\n\n", ca++, dp[(1 << n) - 1]);
}
return 0;
}

POJ 2923 Relocation(状压DP)题解的更多相关文章

  1. POJ 3254 简单状压DP

    没什么可说的,入门级状压DP.直接撸掉 #include <iostream> #include <cstring> #include <cstdlib> #inc ...

  2. POJ 3254 & POJ 1185(状压DP入门)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16773   Accepted: 8860 Desc ...

  3. Corn Fields POJ - 3254 (状压dp)

    题目链接: Corn Fields  POJ - 3254 题目大意:给你一个n*m的矩阵,矩阵的元素只包括0和1,0代表当前的位置不能放置人,1代表当前的位置可以放人,当你决定放人的时候,这个人的四 ...

  4. poj 1185 (状压dp)

    Problem 炮兵阵地 题目大意 给你一张n*m的地图,一些地区是空地,一些地区是障碍. 可以在空地上布置炮兵部队,炮兵部队的攻击范围为上下左右各两格. 询问最多可以布置多少个炮兵部队,且互不伤害. ...

  5. POJ 1185 经典状压dp

    做了很久的题 有注释 #include<stdio.h> #include<string.h> #include<algorithm> #include<ma ...

  6. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  7. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  8. 【POJ 2923】Relocation(状压DP+DP)

    题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...

  9. HDU 2923 Relocation(状压dp+01背包)

    题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...

随机推荐

  1. CF625E Frog Fights

    有\(n\)只青蛙在一个长度为\(m\)的环上打架:每只青蛙有一个初始位置\(p_i\),和一个跳跃数值\(a_i\).从\(1\)号青蛙开始按序号循环行动,每次若第\(i\)只青蛙行动,则它会向前跳 ...

  2. vue首次加载白屏过渡动画(vue优化)

    过渡动画需要在index.html文件里面添加 1.css,在public.index.css创建index.css html, body, #app { height: 100%; margin: ...

  3. Go Code Review Comments

    Go Code Review Comments https://golang.org/wiki/CodeReviewComments

  4. 源码 redis 分布式锁

    https://github.com/SPSCommerce/redlock-py/tree/master/redlock

  5. 用Jenkins构建Django持续集成环境

    用Jenkins构建Django持续集成环境 - V2EX https://www.v2ex.com/t/32054

  6. SVN 常见问题及解决方法

    1.已跳过 'XXX' -- 节点处于冲突状态 解决方法:cd 进入到出错文件夹或文件所在目录,执行: svn resolved ./文件名(文件夹名)

  7. Okio Okio源码分析

    概述 Okio 作为 Okhttp 底层 io 库,它补充了 java.io 和 java.nio 的不足,使访问.存储和处理数据更加容易.Okio 的特点如下: okio 是一个由 square 公 ...

  8. MySQL的索引为什么用B+Tree?InnDB的数据存储文件和MyISAM的有何不同?

    前言 这篇文章的题目,是我真实在面试过程中遇到的问题,某互联网众筹公司在考察面试者MySQL相关知识的第一个问题,我当时还是比较懵的,没想到这年轻人不讲武德,不按套路出牌,一般的问MySQL的相关知识 ...

  9. Spring Boot 2.x基础教程:使用JTA实现多数据源的事务管理

    在一个Spring Boot项目中,连接多个数据源还是比较常见的.之前也介绍了如何在几种常用框架的场景下配置多数据源,具体可见: Spring Boot 2.x基础教程:JdbcTemplate的多数 ...

  10. 如何学习Java?从标识符开始

    标识符 1.定义 Java对各种变量.方法和类等要素命名时使用的字符序列称为标识符(包含但不限于:类名.变量名.方法名.接口名.包名--) 2.命名规则 1.由26个英文字母大小写,0-9,_或$组成 ...