• 题目1455:珍惜现在,感恩生活(多重背包问题)


    题目链接:http://ac.jobdu.com/problem.php?pid=1455

    详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

    参考代码:

    //
    //  1455 珍惜现在,感恩生活.cpp
    //  Jobdu
    //
    //  Created by PengFei_Zheng on 25/04/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cmath>
    #define MAX_SIZE 101
    #define MAX_NUM 2001
    using namespace std;
     
    int C, n, m, price, weight, num;
     
    struct Rice{
        int price;
        int weight;
    };
     
    int dp[MAX_SIZE];
    Rice rice[MAX_NUM];
     
    int main(){
        scanf("%d",&C);//C means the number of total test cases
        while(C--){
            scanf("%d%d",&n,&m);// n means the money yout have, m means there are m different kinds of rice in market.
            int cnt = 0;//divide the rice into different heap
            for(int i = 1 ; i <= m ; i++){
                scanf("%d%d%d",&price,&weight,&num);//input each kind of rice's price, weight, num
                int x = 1;
                while(num-x>0){
                    num-=x;
                    cnt++;
                    rice[cnt].price = x * price;
                    rice[cnt].weight = x * weight;
                    x*=2;
                }
                cnt++;
                rice[cnt].price = num * price;
                rice[cnt].weight = num * weight;
            }
            //initation the dp[j] means: with money j the total weight rice you can buy.
            for(int i = 1 ; i <= n ; i++){
                dp[i]=0;
            }
            //change the problem into 0-1 backpage
            for(int i = 1 ; i <= cnt ; i++){
                for(int j = n ; j >= rice[i].price ; j--){
                    dp[j] = max(dp[j],dp[j-rice[i].price]+rice[i].weight);
                }
            }
            printf("%d
    ",dp[n]);
        }
        return 0;
    }
    /**************************************************************
        Problem: 1455
        User: zpfbuaa
        Language: C++
        Result: Accepted
        Time:10 ms
        Memory:1536 kb
    ****************************************************************/
  • 相关阅读:
    servlet学习之servletAPI编程常用的接口和类
    问题解决
    HTTP Status 500 – Internal Server Error
    用数组模拟队列
    稀疏数组
    值传递机制及几道网红题目
    关于Tomcat配置问题
    Servlet学习笔记
    面向对象笔记
    数组中涉及的常见算法
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6763894.html
Copyright © 2020-2023  润新知