您的位置:首页 > 其它

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

2016-05-20 18:04 369 查看
Relocation

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 3023Accepted: 1226
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:

At their old place, they will put furniture on both cars.
Then, they will drive to their new place with the two cars and carry the furniture upstairs.
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
n, C1 and C2. C1 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 t(保证)hat 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的火车装载,问最少需要运送多少次可以将货物运完。

分析: 找出所有状态(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: