POJ-2923 Relocation(状压+背包)

题意:

2 2 2辆车体积分别为 c 1 、 c 2 c1、c2 c1c2以及 n n n个物品和其对应的体积 a [ i ] a[i] a[i],现在要用这两辆车运完这些物品,运物品的体积不能超过车体积(物品可以分成几份来运),问最少几次能运完。

分析:

枚举所有可行的运输方式作为状态 s t a [ i ] sta[i] sta[i],然后用这些状态做一次 01 01 01背包。
d p [ i ] dp[i] dp[i]在二进制下 1 1 1表示运完了这个物品, 0 0 0表示没运完, d p [ ( 1 &lt; &lt; n ) − 1 ] dp[(1&lt;&lt;n)-1] dp[(1<<n)1]就是运完的状态。
转移方程: d p [ j ∣ s t a [ i ] ] = m i n ( d p [ j ∣ s t a [ i ] ] , d p [ j ] + 1 ) dp[j|sta[i]]=min(dp[j|sta[i]], dp[j]+1) dp[jsta[i]]=min(dp[jsta[i]],dp[j]+1)

枚举所有可行状态时,如果运的物品体积已经超过 c 1 + c 2 c1+c2 c1+c2则这个状态 n o no no,否则根据这个状态的总体积和可行的 c 1 c1 c1来确定有没有 c 2 c2 c2和其对应,有对应则状态 o k ok ok

#include <queue>
#include <stack>
#include <cmath> 
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int maxn  = 1024 + 5;
const int maxm  = 100 + 5;
const int inf   = 0x3f3f3f3f;
const LL  mod   = 1e9 + 7;//19260817
const double pi = acos(-1.0);

int n, c1, c2, cnt, cas, maxsta, sta[maxn], dp[maxn], a[15];
bool vis[maxn];

bool check(int x){
	int sum = 0;
	memset(vis, false, sizeof vis);
	vis[0] = true;
	for(int i = 1; i <= n; i++){
		if((1 << (i - 1)) & x){
			sum += a[i];
			for(int j = c1; j >= a[i]; j--) if(vis[j - a[i]])
				vis[j] = 1;
		}
	}
	if(sum > c1 + c2) return false;
	for(int i = 0; i <= c1; i++) if(vis[i] && sum - i <= c2){
		return true;
	}
	return false;
}

int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		scanf("%d %d %d", &n, &c1, &c2);
		for(int i = 1; i <= n; i++) scanf("%d", a + i);
		memset(dp, inf, sizeof dp);
		maxsta = 1 << n;
		cnt = 0;
		for(int i = 0; i < maxsta; i++){
			if(check(i)) sta[++cnt] = i;
		}
		dp[0] = 0;//没有物品运0次
		for(int i = 1; i <= cnt; i++){
			for(int j = maxsta; ~j; j--) if(dp[j] != inf){
				dp[j | sta[i]] = min(dp[j | sta[i]], dp[j] + 1);
			}
		}
		printf("Scenario #%d:\n", ++cas);
		printf("%d\n\n", dp[maxsta - 1]);
	}
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值