Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3539 | Accepted: 1457 |
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 capacitiesC1 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) andn is the number of pieces of furniture (1 ≤ n ≤ 10). The following line will containn 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:”, 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
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
Source
[Submit] [Go Back] [Status] [Discuss]
题意:两辆车n个物品,每个物品有体积,两辆车也有体积,要求把物品全部运走最少需要多少次,每次每辆车运送的物体总体积不得大于车的体积
第一次接触状态压缩,看了一下大神的题解:Link
分析:
物品个数最多是10个,可以用0和1分别表示物品是否被选中运送;
假设有3个物品,那么可以用001表示选择将第3个物品运走;
那么,所有的状态可以用0~2^n-1对应的二进制数表示出来;
对于上述每一种状态,选择其中可以一次运走的状态进行01背包;
值得注意的是,因为一件物品不会被运2次,所以所有选取的状态应该是没有交集的,比如:
1001101和1110010是不行的,第一件物品被运了2次;
然后为了使次数最小,可以将次数抽象成背包中的物品价值,每种状态对应运送一次,价值是1
AC code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define eps 1
const int mod=1000000000+7;
const int maxn=2000+5;
int v[maxn];
int vis[maxn];
int cnt;
int n,c1,c2;
int dp[maxn];
int st[maxn];
int Judge(int x){
memset(vis,0,sizeof vis);
int sum=0;
vis[0]=1;
for(int i=0;i<n;i++){
if((x>>i)&1){
sum+=v[i]; //计算总量
for(int j=c1;j>=v[i];j--){ //计算第一辆车可以装的组合
if(vis[j-v[i]]){ //表示前面标记过
vis[j]=1; //vis[j]表示存在可以装j容量的物品
}
}
}
}
if(sum>c1+c2) return 0;
else{
for(int i=0;i<=c1;i++){
if(vis[i] && sum-i<=c2)
return 1;
}
return 0;
}
}
int init(int n){
cnt=0;
for(int i=1;i<(1<<n);i++){
dp[i]=INF;
if(Judge(i)){
st[cnt++]=i;
}
}
}
int main() {
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
scanf("%d%d%d",&n,&c1,&c2);
for(int i=0;i<n;i++){
scanf("%d",&v[i]);
}
init(n);
int maxv=(1<<n)-1;
dp[0]=0;
for(int i=0;i<cnt;i++){
for(int j=0;j<=maxv;j++){
if((j&st[i])==0){ //位不能相同
dp[j|st[i]]=min(dp[j|st[i]],dp[j]+1);
}
}
}
printf("Scenario #%d:\n%d\n",cas,dp[maxv]);
if(cas!=t) printf("\n");
}
}