POJ 2923 Relocation(状态压缩+01背包)

来源:互联网 发布:raphael.js 中文api 编辑:程序博客网 时间:2024/05/01 19:16

Relocation
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3023 Accepted: 1226

Description

Emma and Eric are moving to their new house they bought after returning from theirhoneymoon(蜜月). Fortunately, they have a few friends helping themrelocate(浮动). To move the furniture, they only have twocompact(紧凑的) cars, whichcomplicates(复杂) 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. Theschedule(时间表) 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 thecapacities(能力)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 capacityC, the sum of the weights of all the furniture it loads for one trip can be at mostC.

Input

The first line contains the number of scenarios(方案). Each scenario consists of one line containing three numbersn, C1 and C2. C1 andC2 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 t(保证)hat each piece of furniture can be loaded by at least one of the two cars.

Output

The output(输出) for everyscenario(方案) begins with a line containing “Scenario #i:”, wherei 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

26 12 133 9 13 3 10 117 1 1001 2 33 50 50 67 98

Sample Output

Scenario #1:2Scenario #2:3

题意: 有 n 个货物,并且知道了每个货物的重量,每次用载重量分别为c1,c2的火车装载,问最少需要运送多少次可以将货物运完。

分析: 找出所有状态(1.....(1<<n)-1)中选出可以用两辆车一次运完的状态

        把每个状态都看作一个物品,重量为该状态的总重量,价值为 1

        求解 01 背包,dp[(1<<n)-1]为最优解

        转移方程: dp[stat|j]=min(dp[stat|j],dp[j]+1)  注意 stat 和 j 不能有交集



#include <stdio.h>#include <string.h>#define min(a,b) (a)<(b)?(a):(b)int dp[1<<11];int w[15];int a[1<<11];bool v[1<<11];int n, c1, c2;bool ok(int x){memset(v, false, sizeof(v));v[0] = true;int s = 0;int i, j;for(i = 0; i < n; i++){if((1 << i) & x){s += w[i];if(s > c1 + c2)return false;for(j = c1; j >= w[i]; j--)if(v[j - w[i]])v[j] = true;}}for(i = c1; i >= 0; i--){if(v[i] && s - i <= c2)return true;}return false;}int main(){int t, i, j, k, top, st;scanf("%d", &t);for(i = 1; i <= t; i++){memset(dp, 0x3f, sizeof(dp));dp[0] = 0;scanf("%d%d%d", &n, &c1, &c2);for(j = 0; j < n; j++)scanf("%d", &w[j]);st = (1 << n) - 1;top = 0;for(j = 1; j <= st; j++)if(ok(j))a[top++] = j;for(j = 0; j < top; j++){for(k = st; k >= 0; k--){if(!(k & a[j]))dp[a[j] | k] = min(dp[a[j] | k], dp[k] + 1);}}printf("Scenario #%d:\n%d\n\n", i, dp[st]);}return 0;}


0 0
原创粉丝点击