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 }
  • 相关阅读:
    C#和C++除了语法上的差别外,还有什么其他的区别
    各种指针的的概览及造成原因
    批量操作Tomcat Shell脚本
    pi币pinetwork安装注册教程中文详细版【实操有效】
    Oracle分析函数
    Logger.Xml
    使用Redis / Zookeeper作为分布式锁的一些注意点
    Seata Server配置文件
    .gitignore忽略target无效
    MySql隔离级别:RU / RC / RR / S + 脏读 / 不可重复读 / 幻读 / 可重复读
  • 原文地址:https://www.cnblogs.com/luomi/p/5965258.html
Copyright © 2011-2022 走看看