HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923
题目链接:http://poj.org/problem?id=2923
Relocation
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3472 | Accepted: 1422 |
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 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
题目大意:搬家只有两辆车运家具,两辆车要一起行动,并且两辆车的最大载重分别为c1,c2,有n件家具,给出了n件家具分别的重量,问要最少多少次才能运完所有家具。
题目分析:第一次接触状压dp的问题,纠结了很久也不知道状态压缩的实质与意义是什么,弄了整整一天才把这道题ac,哎~(心情复杂脸),咳咳,不扯淡了,开始正题。其实这道题状态压缩的实质就是用二进制的1和0代表物品的有无,比如说二进制011代表的是整数3,在状态压缩中的含义是整数3所代表的状态中,有第一和第二件物品,所以用二进制的方式来表达物品的存在状态是很方便的事情。
注意:输出时每两组数据都需要一个额外的空行(POJ中的题目可以在每组数据的末尾添加两个换行,但是UVA中的不允许返回的是格式错误)
PS:这是一道状态压缩好题,多研究有好处
AC代码:
//# define FLAG
///delet....................................
# include <iostream>
# include <cstring>
# include <cstdlib>
# include <cstdio>
# include <string>
# include <cmath>
# include <ctime>
# include <set>
# include <map>
# include <queue>
# include <stack>
# include <bitset>
# include <vector>
# include <fstream>
# include <algorithm>
using namespace std;
# define eps 1e-8
# define pb push_back
# define mp make_pair
# define pi acos(-1.0)
# define bug puts("H");
# define mem(a,b) memset(a,b,sizeof(a))
# define IOS ios::sync_with_stdio(false);
# define FO(i,n,a) for(int i=n; i>=a; --i)
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define MAX 0x7fffffffffffff
# define INF 0x3f3f3f3f
# define MOD 1000000007
/// 123456789
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef unsigned long long ULL;
typedef long long LL;
inline int Scan(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
///coding...................................
const int MAXM=(1<<15);
int a[15],b[MAXM],c[MAXM],dp[MAXM],vis[MAXM],n,c1,c2;
int main()
{
IOS
#ifdef FLAG
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif /// FLAG
int t,cnt=0;
cin>>t;
while(t--) {
int n,c1,c2;
cin>>n>>c1>>c2;
for(int i=0;i<n;++i)
cin>>a[i];
int sum,t1=0,t2=0;
for(int i=0;i<(1<<n);++i) { ///遍历所有可能的方案
sum=0;
for(int j=0;j<n;++j) ///遍历i所代表的状态中,拥有物品的总重量
if(i&(1<<j))sum+=a[j];
if(sum<=c1)b[t1++]=i; ///如果满足重量小于c1则为一种可选择的方案
if(sum<=c2)c[t2++]=i; ///同上
}
int t=0;
for(int i=0;i<t1;++i)
for(int j=0;j<t2;++j)
if((b[i]&c[j])==0)vis[t++]=(b[i]|c[j]); ///确保两个方案中没有相同的物品
for(int i=0;i<(1<<n);++i)dp[i]=INF; ///初始化
dp[0]=0;
for(int i=0;i<t;i++)
for(int j=(1<<n)-1-vis[i];j>=0;j--)
if((j&vis[i])==0)
dp[j|vis[i]]=min(dp[j|vis[i]],dp[j]+1);
if(cnt)puts("");
printf("Scenario #%d:\n%d\n",++cnt,dp[(1<<n)-1]);
}
return 0;
}
///delete FLAG..............................