题目1455:珍惜现在,感恩生活(多重背包问题)
Posted 伊甸一点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题目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\n",dp[n]); } return 0; } /************************************************************** Problem: 1455 User: zpfbuaa Language: C++ Result: Accepted Time:10 ms Memory:1536 kb ****************************************************************/
以上是关于题目1455:珍惜现在,感恩生活(多重背包问题)的主要内容,如果未能解决你的问题,请参考以下文章
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(复习多重背包)
hdu-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活---多重背包