• ***C


    C - I love sneakers!
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.

    There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
    Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
    Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.
     

    Input

    Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.
     

    Output

    For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn's demands can’t be satisfied.
     

    Sample Input

    5 10000 3
    1 4 6
    2 5 7
    3 4 99
    1 55 77
    2 44 66
     

    Sample Output

    255
     题意:一个人去买运动鞋,他手里拥有的钱为m,运动鞋有k种品牌,每种品牌都可能有多双运动鞋,并且对于每种品牌,他都至少要买一双。给出n双运动鞋,运动鞋的品牌号,价格,价值分别为a,b,c。问他用那些钱去买运动鞋,能获得的最大价值是多少。
    思路:

    1. 要求每一组之中至少有一个被选中,和之前的最多有一个稍有区别,需要把题目再次细分。

    2. dp[j][w] 表示选定 j 个品牌并且花费w的最大价值,dp[j]w] 为正数表示状态存在,为负数表示状态不存在。

    3. dp[j][w] = max(dp[j][w], dp[j][w - s[i].b] +s[i].c);  第j类品牌有选择并且要选择第i件物品。(不选择第i件物品是相等的,所以略过转移方程)

       dp[j][w] = max(dp[j][w], dp[j - 1][w- s[i].b] +s[i].c);  第j类品牌前面没有选择并且要选择第i件物品。

    4. 由于状态是从 0 到 i 的且 dp[0][w] = 0,其他为 -INF。所以只有当第一类品牌的状态存在时,才能推导出来第二类品牌的存在状态,以此类推。

    5. 题目中有 2 个陷阱,一是可能会存在某品牌数量为 0 的情况,另外会存在费用或价格为 0 的情况,所以状态转移方程不能调换。

    AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define INF 0x7fffffff
     6 
     7 
     8 using namespace std;
     9 struct ks{
    10     int id;
    11     int b;
    12     int c;
    13 };
    14 ks s[103]={0};
    15 int dp[22][10005]={0};
    16 int number[13]={0};
    17 bool cmp(ks a1,ks a2){
    18     if(a1.id==a2.id)
    19         return a1.b<a2.b;
    20     else
    21         return a1.id<a2.id;
    22 }
    23 
    24 int main()
    25 {
    26     freopen("1.txt","r",stdin);
    27     int n,money,k;
    28     int a;
    29     int i,j;
    30     while(cin>>n>>money>>k){
    31         for(i=0;i<n;i++){
    32         cin>>s[i].id>>s[i].b>>s[i].c;//录入信息
    33     }
    34     sort(s,s+n,cmp);//按产品号和价格的升序进行排列
    35     for(i=0;i<k;i++){
    36         for(j=0;j<=money;j++)
    37             dp[i][j]=-INF;
    38     }
    39     dp[0][0]=0;//重置为0,为了第一重循环时的使用
    40     int w;
    41     j=0;
    42     for(i=0;i<n;i++){
    43         if(i&&s[i-1].id!=s[i].id)
    44             j++;//记录鞋的品牌种数
    45         for(w=money;w>=s[i].b;w--)//一定要从后向前推否则会取重的
    46             {//技巧性的实现了至少购买1双鞋子
    47             if(dp[j][w-s[i].b]!=-INF&&dp[j][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子已经买过了,并且买下第i双鞋子;
    48                 dp[j][w]=dp[j][w-s[i].b]+s[i].c;
    49             if(j&&dp[j-1][w-s[i].b]!=-INF&&dp[j-1][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子没有买过,并且买下第i双鞋子;
    50                 dp[j][w]=dp[j-1][w-s[i].b]+s[i].c;
    51         }
    52     }
    53     int ans=-1;
    54     for(i=money;i>=0;i--){
    55         if(ans<dp[j][i])
    56             ans=dp[j][i];
    57     }
    58     if(ans==-1||j<k-1)
    59         cout<<"Impossible"<<endl;
    60     else
    61         cout<<ans<<endl;
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    操纵持久化对象
    面阵和线扫工业相机选型
    线扫描镜头简介及选型
    Halcon的anisotropic_diffusion()函数,用于对图像进行各向异性散射增强处理
    VB、C#等高级语言与三菱PLC(Q系列、L系列、FX系列、A系列)串口、以太网通讯的DLL及源代码
    Halcon学习笔记之支持向量机
    C#中使用byte[]数据,生成Bitmap(256色 灰度 BMP位图)源代码
    Halcon学习SVM
    利用MLP(多层感知器)创建一个新的OCR分级器
    Halcon中OCR的实现及关键函数解析
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3354238.html
Copyright © 2020-2023  润新知