zoukankan      html  css  js  c++  java
  • hdu 3033 I love sneakers!(分组背包+每组至少选一个)

    I love sneakers!

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3265    Accepted Submission(s): 1337


    Problem 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
     
    Source
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  2639 1712 3535 2415 3449 
     
     1 //93MS    844K    1142 B    C++
     2 /*
     3     
     4     题意:
     5         有n双鞋子,m块钱,k种牌子,每双鞋子属于某个牌子,价格为b,价值为c,
     6     现在问在满足在现有的金钱内买鞋,每个牌子的鞋最少有一双的情况下获得的最大价值,
     7     不能满足则输出 Impossible
     8     
     9     背包:
    10         分组背包,并且每组至少一个。与传统的分组背包有点不同,难点是判断每组至少一个的
    11     情况,此处采用标记法,先初始化 dp数组为-1,dp[0][0..V]为0,状态转移时判断前一状态
    12     是否可行,可行在进行转移。 
    13      
    14 */ 
    15 #include<iostream>
    16 #include<vector>
    17 using namespace std;
    18 struct node{
    19     int v;
    20     int w;
    21     node(int a,int b){
    22         v=a;w=b;
    23     }
    24 };
    25 vector<node>V[105];
    26 int dp[15][10005];
    27 int n,m,k;
    28 int main(void)
    29 {
    30     int a,b,c;
    31     while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    32     {
    33         memset(dp,-1,sizeof(dp));
    34         for(int i=0;i<=m;i++) dp[0][i]=0; //初始化 
    35         for(int i=0;i<=k;i++) V[i].clear();
    36         for(int i=0;i<n;i++){
    37             scanf("%d%d%d",&a,&b,&c);
    38             V[a].push_back(node(b,c));
    39         }
    40         for(int i=1;i<=k;i++){
    41             int n0=V[i].size();
    42             for(int j=0;j<n0;j++){
    43                 for(int v=m;v>=V[i][j].v;v--){
    44                     if(dp[i][v-V[i][j].v]!=-1) //当前牌子转移状态 
    45                         dp[i][v]=max(dp[i][v],dp[i][v-V[i][j].v]+V[i][j].w);
    46                     if(dp[i-1][v-V[i][j].v]!=-1) //前一牌字转移状态 
    47                         dp[i][v]=max(dp[i][v],dp[i-1][v-V[i][j].v]+V[i][j].w);
    48                 }
    49             }
    50         }
    51         if(dp[k][m]==-1) puts("Impossible");
    52         else printf("%d
    ",dp[k][m]);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    java 大数据比较两个list集合的差值
    service手动回滚
    mycat分库分表demo
    mysql集群搭建之读写分离
    Python 代码运行速度慢?用PyPy模块让你的速度起飞!
    大新闻!Python 之父重新出山,加入微软开发部
    附实战代码|告别OS模块,体验Python文件操作新姿势!
    Python九个最佳IDE集成开发环境,你用过吗?
    五个阶段让你成为Python专家,你确定不看看吗?
    制作交互式数据可视化图表,只需要一个Python库就能实现!
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3422256.html
Copyright © 2011-2022 走看看