Input

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。

Output

对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

Sample Input

1

8 2

2 100 4

4 100 2

Sample Output

400

【分析】:

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int MAXN = 1e3 + 5;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n,m,k; int num[maxm];
int v[maxm], w[maxm],dp[maxm];
int main()
{
int t;
cin>>t;
while(t--)
{
ms(dp,0);
cin>>m>>n;
for(int i=0;i<n;i++){
cin>>w[i]>>v[i]>>num[i];
}
for(int i=0;i<n;i++){
for(int k=1; k<=num[i]; k++){
for(int j=m; j>=w[i]; j--){
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
}
}
}
cout<<dp[m]<<endl;
}
return 0;
}
/*
1
8 2
2 100 4
4 100 2 Sample Output
400
*/

【二进制优化】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int MAXN = 1e3 + 5;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n,m,k; int num[maxm];
int v[maxm], w[maxm],dp[maxm];
void zero(int cost ,int value)
{
for(int j=m;j>=cost;j--){
dp[j]=max(dp[j],dp[j-cost]+value);
}
} void cmp(int cost, int value)
{
for(int j=cost;j<=m;j++){
dp[j]=max(dp[j],dp[j-cost]+value);
}
}
void mul(int cost, int value,int cnt)
{
if(m<=cnt*cost) {
cmp(cost,value);
return ;
}
else{
int k=1;
while(k<=cnt){
zero(k*cost,k*value);
cnt-=k;
k<<=1;
}
}
zero(cnt*cost,cnt*value);
}
int main()
{
int t;
cin>>t;
while(t--)
{
ms(dp,0);
cin>>m>>n;
for(int i=1;i<=n;i++){
cin>>w[i]>>v[i]>>num[i];
}
for(int i=1;i<=n;i++){
mul(w[i],v[i],num[i]);
} cout<<dp[m]<<endl; }
return 0;
}
/*
1
8 2
2 100 4
4 100 2 400
*/

最新文章

  1. html5中canvas的使用 获取鼠标点击页面上某点的RGB
  2. js变量问题
  3. Python自动化之sqlalchemy(修改和查询)
  4. JS调用BHO
  5. WP8.1 Study4:WP8.1中控件集合应用
  6. Shell脚本文件中常用的操作语句
  7. TCP/IP协议族-----13、运输层简单介绍
  8. 本部校赛 蛇形填数(二)problen1338
  9. UBER人民优步司机注册攻略
  10. lightoj 1179(线段树)
  11. 下拉框的change事件
  12. [ExtJS5学习笔记]第七节 Extjs5的组件components及其模板事件方法学习
  13. BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案
  14. JavaScript学习day2 (基本语法上)
  15. 第59节:Java中的html和css语言
  16. 翻译:非递归CTE(已提交到MariaDB官方手册)
  17. 20172328《程序设计与数据结构》实验四 Android程序设计报告
  18. [硬件]Robot运动控制
  19. string的方法find
  20. mybatis 之引入多个model

热门文章

  1. 在iis上部署asp.net mvc2.0
  2. Git上手:Git扫盲区
  3. CentOS6/7-防火墙管理
  4. python 学习分享-实战篇高级的ftp
  5. [类和对象]3 C++面向对象模型初探
  6. Android 启动项 Activity
  7. [ecmagent][redis学习][1初识redis] python操作redis
  8. SQL小助手——SQL Prompt
  9. axis2实践(一)JAX-WS入门示例
  10. [HNOI2015][bzoj4011] 落叶枫音 [拓扑DP]