zoukankan      html  css  js  c++  java
  • Relocation 状态压缩DP

     Relocation
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <map>
     5 using namespace std;
     6 int c1,c2,vi[110],a[30],n,dp[1100];
     7 bool check(int x)
     8 {
     9     int i,j,sum=0;
    10     memset(vi,0,sizeof(vi));
    11     vi[0]=1;
    12     for(i=0;i<n;i++)
    13     {
    14         if(x&(1<<i))
    15         {
    16             sum+=a[i];
    17             for(j=109;j>=a[i];j--)
    18             if(vi[j-a[i]])
    19             vi[j]=1;
    20         }
    21     }
    22     for(i=c1;i>=0;i--)
    23     {
    24         if(vi[i]&&sum-i<=c2)return 1;
    25     }
    26     return 0;
    27 }
    28 int main()
    29 {
    30     int t,i,j,b[(1<<11)],bn,sum,cas=1;
    31     scanf("%d",&t);
    32     while(t--)
    33     {
    34         sum=0;
    35         scanf("%d%d%d",&n,&c1,&c2);
    36         if(c1>c2)swap(c1,c2);
    37         for(i=0;i<n;i++)
    38         scanf("%d",&a[i]),sum+=a[i];
    39         bn=0;
    40         for(i=1;i<(1<<n);i++)
    41         {
    42             if(check(i))
    43             b[bn++]=i;
    44         }
    45         for(i=0;i<1<<n;i++)dp[i]=20;
    46         dp[0]=0;
    47         for(i=0;i<bn;i++)
    48         {
    49             for(j=(1<<n)-1;j>=0;j--)
    50             if((b[i]|j)==j)
    51             dp[j]=min(dp[j],dp[b[i]^j]+1);
    52         }
    53         cout<<"Scenario #"<<cas++<<":"<<endl;
    54         cout<<dp[(1<<n)-1]<<endl<<endl;
    55     }
    56 }
    View Code
  • 相关阅读:
    Cocos2dx 3.0 过渡篇(二十五)死不了的贪食蛇(触摸版)
    IBinder对象在进程间传递的形式(一)
    【代码实现】PHP生成各种随机验证码
    win8 metro 调用摄像头拍摄照片并将照片保存在对应的位置
    薏米红豆粥功效及做法介绍
    应用程序无法正常启动0xc0150002 解决方式
    贪心算法
    Linux 进程通信之 ——信号和信号量总结
    关于 ioctl 的 FIONREAD 參数
    google域名邮箱申请 gmail域名邮箱申请(企业应用套件)指南
  • 原文地址:https://www.cnblogs.com/ERKE/p/3841959.html
Copyright © 2011-2022 走看看