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 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

Source

TUD Programming Contest 2006, Darmstadt, Germany

两辆车运家具,先把所有能一次运走的家具组合用状压找出来,后用背包在所有的组合找最少的运的次数

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=5000;
const int inf=0x7ffffff;
int dp[maxn],vis[maxn],a[maxn];
int w[maxn];
int n,c1,c2;
bool check(int x)//判断当前状态下是否能够一次运完
{
    int sum=0;
    memset(vis,0,sizeof(vis));
    vis[0]=1;
    for(int i=0;i<n;i++)
    {
        if(x&(1<<i))
        {
           sum+=w[i];
           for(int j=c1-w[i];j>=0;j--)//01背包
           {
               if(vis[j])
               vis[j+w[i]]=1;
           }
        }
    }
    for(int i=c1;i>=0;i--)
    {
        if(vis[i]&&sum-i<=c2)
        return true;
    }
    return false;
}
int main()
{
    int t,sign=0,count=1;
    scanf("%d",&t);
    while(t--)
    {
        if(sign)
        printf("\n");
        else
        sign=1;
        scanf("%d%d%d",&n,&c1,&c2);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&w[i]);
        }
        int cur=(1<<n)-1;
        int cnt=0;
        //遍历所有的状态,将所有可以一次运完的储存在a数组内
        for(int i=0;i<=cur;i++)
        {
            if(check(i))
            a[cnt++]=i;
        }
        for(int i=0;i<maxn;i++)
        dp[i]=inf;
        dp[0]=0;
        for(int i=0;i<cnt;i++)//背包
        {
            for(int j=cur-a[i];j>=0;j--)
            {
                dp[j|a[i]]=min(dp[j|a[i]],dp[j]+1);
            }
        }
        printf("Scenario #%d:\n%d\n",count++,dp[cur]);
    }
    return 0;
}
时间: 2024-10-29 19:06:24

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

题目大意:有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一下:为什么可以看成是物品?并一下就可以?是

POJ 2688 BFS+状压DP

标准的TSP问题 m*n矩阵,有不超过10个需要走到的点,给出起点,问走最少的步子把所有点走完 BFS出每个必须走到的点的最短距离 然后状压DP即可 #include "stdio.h" #include "string.h" #include "queue" using namespace std; const int dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1} }; int inf=0x7fffffff; in

POJ 3254 简单状压DP

没什么可说的,入门级状压DP,直接撸掉 #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <map> #include <queue> #include <stac

POJ 1753 BFS+状压

给出4*4的黑白棋盘 可以对任意一个点进行翻转(黑->白,或 白->黑),同时会翻转其相邻的四个点 问最少的步骤使棋盘变成全黑或者全白,状压存状态即可 #include "stdio.h" #include "string.h" #include "queue" using namespace std; int dir[5][2]={ {1,0},{-1,0},{0,1},{0,-1},{0,0} }; char str[10][10

Hie with the Pie(POJ 3311)状压DP

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be

[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

状压 + 背包

Description In a shop each kind of product has a price. For example, the price of a flower is 2 ICU (Informatics Currency Units) and the price of a vase is 5 ICU. In order to attract more customers, the shop introduces some special offers. A special

POJ 2923(状态集合背包)

http://www.cnblogs.com/kuangbin/archive/2012/09/14/2685430.html #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <stac