码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-25 21:32:54      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:max   out   git   namespace   price   struct   scan   ++   ide   

题目链接: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\n",dp[n]);
    }
    return 0;
}
/**************************************************************
    Problem: 1455
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1536 kb
****************************************************************/

 

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

标签:max   out   git   namespace   price   struct   scan   ++   ide   

原文地址:http://www.cnblogs.com/zpfbuaa/p/6763894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!