POJ 2923 Relocation

题目大意:有n个物品,有两辆车载重分别是c1,c2.问需要多少趟能把物品运完。 (1 ≤ Ci ≤ 100,1 ≤ n ≤ 10,1 ≤ wi ≤ 100).

题解:n小思状压。我们先把所有一次可以拉走的状态ini预处理好,然后把这些状态当做一个个物品跑背包就行了。

合并的转移:dp[j|ini[i]]=min(dp[j|ini[i]],dp[j]+1);

就是如果状态j与ini[i]不冲突,则可以从状态j运一趟变为j|ok[i]。

然后正确性可以YY一下:为什么可以看成是物品?并一下就可以?是因为ini中包含了所有的可能情况,我们也全都考虑了转移(这句话看不懂就算了我语文不好= =)

注意几个细节:在check的时候随时注意sm剪枝,f的边界是f[0]=true(无论c1,c2是多少重为0的东西总是可以的),dp的边界是dp[0]=0(全都不搬运能收获0),还有注意判重。然后就差不多了。

不过我还有个问题:为什么inf=-1u>>1就一直爆?

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(‘ ‘)
 8 #define ENT putchar(‘\n‘)
 9 using namespace std;
10 const int maxn=1<<11,maxv=100+10,inf=1<<30;
11 int w[10],n,c1,c2,dp[maxn],ini[maxn];
12 bool check(int val){
13     static bool f[maxv];memset(f,false,sizeof(f));f[0]=true;
14     int sm=0;
15     for(int i=0;i<n;i++) if((1<<i)&val){
16         sm+=w[i];if(sm>c1+c2) return false;
17         for(int j=c1;j>=w[i];j--) f[j]=f[j]|f[j-w[i]];
18     }
19     for(int i=0;i<=c1;i++) if(f[i]&&sm-i<=c2) return true;
20     return false;
21 }
22 inline int read(){
23     int x=0,sig=1;char ch=getchar();
24     while(!isdigit(ch)){if(ch==‘-‘)sig=-1;ch=getchar();}
25     while(isdigit(ch))x=10*x+ch-‘0‘,ch=getchar();
26     return x*=sig;
27 }
28 inline void write(int x){
29     if(x==0){putchar(‘0‘);return;}if(x<0)putchar(‘-‘),x=-x;
30     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
31     for(int i=len-1;i>=0;i--)putchar(buf[i]+‘0‘);return;
32 }
33 void init(){
34     int Case=read();
35     for(int cas=1;cas<=Case;cas++){
36         n=read();c1=read();c2=read();
37         for(int i=0;i<n;i++) w[i]=read();
38         int cnt=0,num=1<<n;
39         for(int i=0;i<num;i++){
40             dp[i]=inf;
41             if(check(i)) ini[cnt++]=i;
42         }
43         dp[0]=0;
44         for(int i=0;i<cnt;i++)
45             for(int j=0;j<num;j++) if(!(ini[i]&j))
46                 dp[ini[i]|j]=min(dp[ini[i]|j],dp[j]+1);
47         printf("Scenario #%d:\n%d\n\n",cas,dp[(1<<n)-1]);
48     }
49     return;
50 }
51 void work(){
52     return;
53 }
54 void print(){
55     return;
56 }
57 int main(){init();work();print();return 0;}
时间: 2024-07-30 16:15:48

POJ 2923 Relocation的相关文章

poj 2923 Relocation 解题报告

题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物品状态:0表示没运走,1表示已被运走. 枚举出两辆车一趟可以运出的状态.由于物品是一趟一趟运出来的.所以就可以由一个状态通过两辆车一趟的状态转移到另一个状态. dp[i]=MIN(dp[k]+1).k可以由两车一趟转移到i. 我是参考此人的:http://blog.csdn.net/bossup/a

[POJ 2923] Relocation (动态规划 状态压缩)

题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开,问最少需要搬运几次? 我先想的是我由A车开始搬,搬运能装的最大的家具总重,然后状态压缩记录下搬运了哪些,然后再由B车搬运,状态压缩记录搬运了哪些.以此类推,直到状态满了. 以上方法TLE 然后,实在想不出来了,看了题解:http://blog.csdn.net/woshi250hua/articl

POJ 2923 Relocation(状压+背包)

Relocation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2340   Accepted: 965 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 r

Relocation POJ - 2923(01背包+状压dp)

Relocation POJ - 2923 原文地址:https://www.cnblogs.com/megadeth/p/11361007.html

状态压缩DP——POJ 2923

对应POJ题目:点击打开链接 Exponentiation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2923 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortu

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

题目大意:有个人需要搬家,有N件物品,给个物品的重量是 w[i] 然后又两个车,每个车的载重量分别是C1和C2,求最少需要运输多少次才能把这些物品全部运输完毕. 分析:刚开始就发现物品数不多,想着直接先枚举一辆车运输的物品,然后计算它运输这些物品需要多少次,不过后来发现复杂度有点高,另一种比较好的解法是先枚举两个车一次可以运走的物品,然后再去用这些可以运走的状态去相互匹配转移,dp[i]表示达到状态i最少的运输次数,求是否能一次运输某个状态也是简单的01背包问题. 代码如下: =========

【POJ 2923】Relocation

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

POJ 2923 状压好题

Relocation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2631   Accepted: 1075 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

poj 2923 状压dp+01背包

好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态集合里的那些物品能否一次就运走,如果能运走,那就把这个状态看成一个物品.预处理完能从枚举中找到tot个物品,再用这tol个物品中没有交集(也就是两个状态不能同时含有一个物品)的物品进行01背包,每个物品的体积是state[i],价值是1,求包含n个物品的最少价值也就是dp[(1<<n)-1](dp