zoukankan      html  css  js  c++  java
  • lightoj1200_完全背包二进制优化

    http://lightoj.com/volume_showproblem.php?problem=1200

    A thief has entered into a super shop at midnight. Poor thief came here because his wife has sent him to buy some necessary households. Instead of buying the items, he decided to steal them.

    He has a bag with him which can carry up to W kg. In the list (given by his wife) there are four fields 1) item name, 2) the price of the item, 3) how many of this item is required and 4) the weight of this item. The items are solid items, so he can't take any item after dividing into pieces.

    Now the thief wants to take items in his bag such that it fulfills his wife's list, and the total weight of the items is not greater than W. And he can't take any item other than the items mentioned in the list, otherwise his wife would found the item and he may get caught. But he can take more items than required. And he wants to sell these extra items and earn some money. Assume that he can take any item (is in the list) any number of times unless they overflow his bag.

    Now you are given the necessary information of the items and his bag, you have to find the maximum profit the thief can make.

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case starts with a line containing two integers n (1 ≤ n ≤ 100) and W (1 ≤ W ≤ 10000), where n denotes the number of items. Each of the next n lines contains three integers pi ci wi (1 ≤ pi, ci, wi ≤ 100), meaning that the price of the ith item is pici of it must be taken, and the weight is wi.

    Output

    For each case, print the case number and the maximum profit he can get. If it's impossible to fulfill his wife's requirements, print 'Impossible'.

    Sample Input

    Output for Sample Input

    2

    3 20

    10 1 10

    5 1 5

    1 1 1

    1 10

    10 1 11

    Case 1: 4

    Case 2: Impossible

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cstdio>
     6 #include <vector>
     7 #include <ctime>
     8 #include <queue>
     9 #include <list>
    10 #include <set>
    11 #include <map>
    12 using namespace std;
    13 #define INF 0x3f3f3f3f
    14 typedef long long LL;
    15 
    16 int p[110], c[110], w[110], dp[10005];
    17 int main()
    18 {
    19     int t, n, W;
    20     scanf("%d", &t);
    21     for(int ca = 1; ca <= t; ca++)
    22     {
    23         scanf("%d %d", &n, &W);
    24         for(int i = 1; i <= n; i++)
    25         {
    26             scanf("%d %d %d", &p[i], &c[i], &w[i]);
    27             W -= c[i]*w[i];
    28         }
    29         if(W < 0)
    30         {
    31             printf("Case %d: Impossible
    ", ca);
    32             continue;
    33         }
    34         if(W==0)
    35         {
    36             printf("Case %d: 0
    ", ca);
    37             continue;
    38         }
    39         for(int i = 1; i <= n; i++)
    40             c[i] = W / w[i];
    41         memset(dp, 0, sizeof(dp));
    42         for(int i = 1; i <= n; i++)
    43         {
    44             for(int j = 1; j <= c[i]; j <<= 1)
    45             {
    46                 for(int k = W; k >= j * w[i]; k--)
    47                 {
    48                     dp[k] = max(dp[k-j*w[i]]+j*p[i], dp[k]);
    49                 }
    50                 c[i] -= j;
    51             }
    52             if(c[i])
    53             {
    54                 for(int k = W; k >= c[i] * w[i]; k--)
    55                 {
    56                     dp[k] = max(dp[k-c[i]*w[i]]+c[i]*p[i], dp[k]);
    57                 }
    58             }
    59         }
    60         printf("Case %d: %d
    ", ca, dp[W]);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    ffmpeg 编译IOS静态库
    冲刺总结——第一篇
    openssl基础知识以及部分命令详解
    电子公文传输系统
    第五组课程设计—小组总结
    学习经验总结
    QtCreator:没CDB二进制档可用为二进制格式在'x86windowsmsvc2008pe32bit'"
    printf格式化输出
    DELL T110 安装windows server 2003
    visualSVN+花生壳实现外网访问局域网内SVN
  • 原文地址:https://www.cnblogs.com/luomi/p/5965258.html
Copyright © 2011-2022 走看看