POJ 2923 Relocation(状压DP)题解
题意:有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)题解的更多相关文章
- POJ 3254 简单状压DP
没什么可说的,入门级状压DP.直接撸掉 #include <iostream> #include <cstring> #include <cstdlib> #inc ...
- POJ 3254 & POJ 1185(状压DP入门)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16773 Accepted: 8860 Desc ...
- Corn Fields POJ - 3254 (状压dp)
题目链接: Corn Fields POJ - 3254 题目大意:给你一个n*m的矩阵,矩阵的元素只包括0和1,0代表当前的位置不能放置人,1代表当前的位置可以放人,当你决定放人的时候,这个人的四 ...
- poj 1185 (状压dp)
Problem 炮兵阵地 题目大意 给你一张n*m的地图,一些地区是空地,一些地区是障碍. 可以在空地上布置炮兵部队,炮兵部队的攻击范围为上下左右各两格. 询问最多可以布置多少个炮兵部队,且互不伤害. ...
- POJ 1185 经典状压dp
做了很久的题 有注释 #include<stdio.h> #include<string.h> #include<algorithm> #include<ma ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- 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 ...
- 【POJ 2923】Relocation(状压DP+DP)
题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...
- HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...
随机推荐
- nmap的理解与利用(初级)
在命令窗口下输入命令等待,可以用回车来查看进度 nmap进行探测之前要把域名通过dns服务器解析为ip地址,我们也可以使用指定的dns服务器进行解析. nmap --dns-servers 主机地址 ...
- 使用Spring的RestTemplate进行接口调用
引自:http://www.zimug.com/ 1.常见的http服务的通信方式 经常使用的方式有HttpClient.OkHttp.RestTemplate.其中RestTemplate是一种更优 ...
- Java 8中字符串拼接新姿势:StringJoiner
介绍 StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾.虽然这也可以在StringBuilder类的帮助 ...
- Docker数据目录迁移解决方案
场景 在docker的使用中随着下载镜像越来越多,构建镜像.运行容器越来越多, 数据目录必然会逐渐增大:当所有docker镜像.容器对磁盘的使用达到上限时,就需要对数据目录进行迁移. 如何避免: 1. ...
- [已完结]CMU数据库(15-445)实验2-B+树索引实现(下)
4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...
- WinForm中实现按Enter将光标移动到下一个文本框
首先窗体加载出来是上面这个样子.有五个文本框,我们要实现的功能就是输入姓名后按Enter,使光标直接定位到手机号中. 在页面加载的时候我们就要获取所有文本框控件,并添加回车事件 private voi ...
- jQuery 真伪数组的转换
//真数组转换伪数组 var arr = [1,3,5,7,9]; var obj = {}; [].push.apply(obj,arr); console.log(obj) //伪数组转真数组 v ...
- QQ好友状态,QQ群友状态,究竟是推还是拉? 网页端收消息,究竟是推还是拉?
https://mp.weixin.qq.com/s/KB1zdKcsh4PXXuJh4xb_Zw 网页端收消息,究竟是推还是拉? 原创 58沈剑 架构师之路 2020-12-28 https:/ ...
- FLOYD判圈
转载一篇博客:http://blog.csdn.net/javasus/article/details/50015687 Floyd判圈算法(Floyd Cycle Detection Algorit ...
- java开发工具一个很好的注释模板
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...