题目
Description
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
有N件物品和一个容量为V的背包。第i件物品的重量是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,且价值总和最大。
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
第一行:物品个数N和背包大小M
第二行至第N+1行:第i个物品的重量C[i]和价值W[i]
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
输出一行最大价值。
Sample Input
4 6 1 4 2 6 3 12 2 7
Sample Output
23
思路
这题也太水了,$01$背包 裸题;
但是我看到$Usaco$ 标签的题就想写题解
代码
#include<bits/stdc++.h> #define re register typedef long long ll; using namespace std; inline ll read() { ll a=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();} return a*f; } ll n,m; ll a[6010],w[6010]; ll dp[13010];//记得dp 数组要开和体积一样而不是与 n 范围一样 int main() { n=read(); m=read(); for(re ll i=1;i<=n;i++) { a[i]=read(); w[i]=read();//读入 } for(re ll i=1;i<=n;i++) for(re ll j=m;j>=a[i];j--) dp[j]=max(dp[j],dp[j-a[i]]+w[i]);//01背包板子 for(re ll i=m;i>=1;i--)//逆循环,输出最大权值 if(dp[i]) { printf("%lld ",dp[m]); return 0; }//啊啊 }//^…^