状态压缩DP——POJ 2923

对应POJ题目:点击打开链接

Exponentiation

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
2923

Description

Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything a bit. Since the
furniture does not fit into the cars, Eric wants to put them on top of the cars. However, both cars only support a certain weight on their roof, so they will have to do several trips to transport everything. The schedule for the move is planed like this:

  1. At their old place, they will put furniture on both cars.
  2. Then, they will drive to their new place with the two cars and carry the furniture upstairs.
  3. Finally, everybody will return to their old place and the process continues until everything is moved to the new place.

Note, that the group is always staying together so that they can have more fun and nobody feels lonely. Since the distance between the houses is quite large, Eric wants to make as few trips as possible.

Given the weights wi of each individual piece of furniture and the capacities C1 and C2 of the two cars, how many trips to the new house does the party have to make to move all the furniture? If
a car has capacity C, the sum of the weights of all the furniture it loads for one trip can be at most C.

Input

The first line contains the number of scenarios. Each scenario consists of one line containing three numbers nC1 and C2C1 and C2 are the capacities of the cars (1
≤ Ci ≤ 100) and n is the number of pieces of furniture (1 ≤ n ≤ 10). The following line will contain n integers w1, …, wn, the weights of the furniture (1 ≤ wi ≤
100). It is guaranteed that each piece of furniture can be loaded by at least one of the two cars.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line with the number of trips to the new house they have to make to move
all the furniture. Terminate each scenario with a blank line.

Sample Input

2
6 12 13
3 9 13 3 10 11
7 1 100
1 2 33 50 50 67 98

Sample Output

Scenario #1:
2

Scenario #2:
3

题意:

有n件家具和两辆车(容量分别为c1, c2),某人要搬家,问至少需要几次才能把所有家具运完(两辆车必须同时行驶)。

思路:

n很小,可以用二进制保存1 ~ (1<<n) - 1 种状态,每种状态表示几件家具,比如n = 7时。5这个状态,5用二进制表示为 0000101,可以用它表示为第5件家具和第7件家具被选上(或者从右边数起,表示第1和第3件家具)。之后可以枚举每种状态i,之后用两个循环就可完成。转移方程为:dp[i | j] = min{dp[i | j],  dp[i] + 1}; (j表示j状态能否一次被两辆车运走,其中i & j 要为空)。dp[x]
表示x状态至少需要几次才能拉走,所以dp[(1<<n) - 1] 就是答案。

再来看怎样求一个状态x,使它能一次被两辆车运走 。先根据状态x的二进制中为1的位置进行对应家具求和sum,然后枚举x的子状态,同样据子状态的二进制中为1的位置进行对应家具求和sum1(表示此sum1要被两辆车中的某辆一次拉走),sum2 = sum - sum1。如果sum1和sum2都符合载重。则状态x能一次被两辆车运走。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define M (1<<N)
#define INF (1<<30)
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define SIZE ((1<<n)-1)
int a[N];
int dp[M];
int OK[M];
int c1, c2;

bool Judge(int x)
{
	int i, j, t = 0;
	int sum1 = 0, sum2 = 0, sum = 0;
	for(i=1; i<=x; i=(1<<t)){ /* 对x集合所包含的物品求和 */
		if(i & x) sum += a[t];
		t++;
	}
	if(sum > c1 + c2) return 0;
	for(i=1; i<=x; i++){ /* 枚举x的子集 */
		if((i & x) != i) continue; /* i不是x的子集 */
		sum1 = t = 0;
		for(j=1; j<=i; j=(1<<t)){
			if(j & i) sum1 += a[t];
			t++;
		}
		sum2 = sum - sum1;
		if(sum1 <= c1 && sum2 <= c2) return 1;
		if(sum2 <= c1 && sum1 <= c2) return 1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int T, n, w = 0;
	int i, j, ans;
	scanf("%d", &T);
	while(T--)
	{
		memset(OK, 0, sizeof(OK));
		scanf("%d%d%d", &n, &c1, &c2);
		for(i=0; i<n; i++) scanf("%d", a + i);
		for(i=1; i<M; i++) dp[i] = INF;

		for(i=1; i<=SIZE; i++)
			if(Judge(i)) OK[i] = dp[i] = 1;

		for(i=1; i<=SIZE; i++){
			for(j=1; j<=SIZE; j++){
				if(i & j) continue; /* 交集不为空 */
				if(dp[i] != INF && OK[j])
					dp[i|j] = MIN(dp[i|j], dp[i] + 1);
			}
		}

		printf("Scenario #%d:\n%d\n", ++w, dp[SIZE]);
		if(T) printf("\n");
	}
	return 0;
}
时间: 2024-10-22 14:50:52

状态压缩DP——POJ 2923的相关文章

(状态压缩DP) poj 2978

Colored stones Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1734   Accepted: 819 Description You are given a row of m stones each of which has one of k different colors. What is the minimum number of stones you must remove so that no

状态压缩dp poj 3254 hdu5045

近来感觉状态压缩dp的强大性(灵活利用了二进制运算很关键)...于是做了俩提来看看..毕竟队友是专业的dp,我只是管中窥豹下而已..日后有机会再与之玩耍玩耍...ps:如果上天再给我一次机会,当年我愿意选择状态dp而不是网络流(只针对目前比赛出题潮流) 经典问题,不相邻/禁点方案数问题.poj3254 #include<iostream> #include<cstdio> using namespace std; int n,m; int dp[5000][15]; int yu[

(状态压缩DP) poj 2441

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 3709   Accepted: 1422 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the

(状态压缩dp) poj 3254

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8729   Accepted: 4651 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

poj 2411 Mondriaan&#39;s Dream(状态压缩+dp)

 题意:用1*2砖块铺满n*m的房间. 思路转自:http://www.cnblogs.com/scau20110726/archive/2013/03/14/2960448.html 因为这道题输入范围在11*11之间,所以可以先打表直接输出.......... 状态压缩DP 经典覆盖问题,输入n和m表示一个n*m的矩形,用1*2的方块进行覆盖,不能重叠,不能越出矩形边界,问完全覆盖完整个矩形有多少种不同的方案 其中n和m均为奇数的话,矩形面积就是奇数,可知是不可能完全覆盖的.接着我们来看

POJ 3254 Corn Fields 状态压缩DP

题目链接:http://poj.org/problem?id=3254 思路:状态压缩DP,状态方程为dp[i][j] += (dp[i-1][k]) code: #include <stdio.h> #include <string.h> #define N 500 const int MOD = 100000000; int dp[15][N],ant[N],n,m,k,map[15]; bool ok(int x) { if(x&(x<<1))return

POJ 3691 (AC自动机+状态压缩DP)

题目链接:  http://poj.org/problem?id=3691 题目大意:给定N的致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题思路: 首先说一下AC自动机在本题中的作用. ①字典树部分:负责判断当前0~i个字符组成的串是否包含致病DNA,这部分靠字典树上的cnt标记完成. ②匹配部分:主要依赖于匹配和失配转移关系的计算,这部分非常重要,用来构建不同字符间状态压缩的转移关系(代替反人类的位运算). 这也是必须使用AC自动机而

poj 3254 Corn Fields ,状态压缩DP

题目链接 题意: 一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一头牛都不放也是一种方案) state[i] 表示对于一行,保证不相邻的方案 状态:dp[i][ state[j] ]  在状态为state[j]时,到第i行符合条件的可以放牛的方案数 状态转移:dp[i][ state[j] ] =Sigma dp[i-1][state'] (state'为符合条