poj 2923 状态压缩,01背包

Relocation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 823   Accepted: 326

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
 
  
两辆车去运一堆货物,货物数量小于等于10,问最少需要几趟能把货物全部运到目的地
先把一次能运走的组合都求出来
然后每个组合就相当于一个物品,背包容量就是总的组合数(1<<n-1),用01背包求最小值即可
有两种方法,枚举每个状态,判断是否能一次被两辆车运走,再01背包
View Code
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int state[1024];
int vis[2000];
int dp[1024];
int w[11];
int num,n,c1,c2;
bool canbe_relocated_by_two_cars_in_one_trip(int ts)
{
int i,j;
memset(vis,0,sizeof(vis));
vis[0]=1;
int sum=0;
for(i=0;i<n;i++)
{
if(ts&(1<<i))
{
sum+=w[i];
for(j=c1-w[i];j>=0;j--)
{
if(vis[j]) vis[j+w[i]]=1;
}
}
}
for(i=c1;i>=0;i--)
if(vis[i]&&sum-i<=c2) return true;
return false;
}
int main()
{
int t,ca=1,i,j;
scanf("%d",&t);
while(t--)
{
num=0;
scanf("%d%d%d",&n,&c1,&c2);
if(c1>c2) swap(c1,c2);
for(i=0;i<n;i++) scanf("%d",&w[i]);
for(i=0;i<(1<<n);i++)
if(canbe_relocated_by_two_cars_in_one_trip(i)) state[num++]=i;
memset(dp,127,sizeof(dp));
dp[0]=0;
for(i=0;i<num;i++)//dp[i]表示运走二进制组合数为i的货物最少需要几趟,i表示一个状态
{
for(j=(1<<n)-1-state[i];j>=0;j--)
{
if(!(j&state[i]))//这句话的存在更能体现对dp的理解
{
if(dp[j|state[i]]>dp[j]+1)
dp[j|state[i]]=dp[j]+1;
}
}
}
printf("Scenario #%d:\n%d\n\n",ca++,dp[(1<<n)-1]);
}
return 0;
}
第二种有点小技巧,时间上慢一点
 
  
View Code
#include<cstdio>
#include<cstring>
const int inf = 1000000000;
int state[10000],w[20],dp[2000],tmp[20];
int n,c1,c2,num;
void init()
{
int i,j,k,p,m;
num=0;
scanf("%d%d%d",&n,&c1,&c2);
for(i=0;i<n;i++) scanf("%d",&w[i]);
for(i=0;i<(1<<n);i++)
{
int tot=0;
for(j=0;j<n;j++)
{
if(i&(1<<j)) tmp[tot++]=w[j];
}
for(j=0;j<(1<<tot);j++)//to decide that if a state can be divided into two cars
{
int t1=c1,t2=c2;
for(k=0;k<tot;k++)//将状态i拆分成两个状态,对于每个可能的组合判断能否由两辆车运走
{
if(j&(1<<k)) t1-=tmp[k];
else t2-=tmp[k];
}
if(t1>=0&&t2>=0)
{
state[num++]=i;
break;
}
}
}
}
void solve()
{
int i,j;
dp[0]=0;
for(i=1;i<(1<<n);i++) dp[i]=inf;
for(i=0;i<num;i++)
{
for(j=(1<<n)-1;j>=0;j--)
{
if(dp[state[i]|j]>dp[j]+1)
dp[state[i]|j]=dp[j]+1;
}
}
printf("%d\n\n",dp[(1<<n)-1]);
}
int main()
{
int t,ca=1;
scanf("%d",&t);
while(t--)
{
init();
printf("Scenario #%d:\n",ca++);
solve();
}
return 0;
}



你可能感兴趣的:(poj)