题意:有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. 技术基础 | Apache Cassandra 4.0基准测试

    Apache Cassandra 4.0已经发布了Beta版,这是第一个支持JDK 11及更高JDK版本的Cassandra版本.   时延对于Apache Cassandra用户来说是个显而易见的关 ...

  2. mybatis缓存源码分析之浅谈缓存设计

    本文是关于mybatis缓存模块设计的读后感,关于缓存的思考,关于mybatis的缓存源码详细分析在另一篇文章:https://www.cnblogs.com/gmt-hao/p/12448896.h ...

  3. 小白都看得懂的Javadoc使用教程

    Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...

  4. Service Locator Pattern 服务定位

    https://www.geeksforgeeks.org/service-locator-pattern/ Service Locator Pattern Last Updated: 06-03-2 ...

  5. Excel 常用数据类型、自定义格式、特殊值等等

    常用数据类型: 常规: 常规单元榴格式不包含慑拍H靛的数字格式. 数值: 可以设置小数位数,是否使用千位分割符,以及负数样式 货币: 可以设置小数位数,货币符号,以及负数样式 会计专用: 可以设置小数 ...

  6. Spark练习之action操作开发

    Spark练习之action操作开发 一.reduce 1.1 Java 1.2 Scala 二.collect 2.1 Java 2.2 Scala 三.count 3.1 Java 3.2 Sca ...

  7. 36.Samba 文件共享服务1--安装及配置参数解释

    1.Samba 服务程序现在已经成为在Linux 系统与Windows系统之间共享文件的最佳选择. 1)安装: [root@localhost ~]#yum install samba Loaded ...

  8. npm qs 模块(中文)

    本文基本使用谷歌翻译加上自己的理解,权当加深记忆. npm 简介 qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库.主要维护者:Jordan Harband最初创建者和维护者:TJ Ho ...

  9. jqXHR.fail()回调方法及其参数详细说明

    jqXHR.fail()是一个可供选择的 error 回调选项的构造函数,.fail()方法取代了的过时的.error()方法.从 jQuery 1.5 开始,$.ajax()返回的jqXHR对象 实 ...

  10. 小白搭建WAMP详细教程---apache、mysql、php的整合配置

    Apache与PHP整合 我们之前说过PHP能够解析PHP代码, 可是不服务于apache,apache能够获取接收浏览器的请求, 可是不能处理PHP代码, 要实现动态站点开发,就必须结合apache ...