Charm Bracelet POJ - 3624

来源:互联网 发布:c语言文件读写函数 编辑:程序博客网 时间:2024/06/12 14:04

Charm Bracelet POJ - 3624

题目描述

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.

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

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背包问题,dp

下面是代码

#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;using namespace std;int max(int x,int y){    return x>y?x:y;}int main(){    int n,m,date[3403][2];    int dp[20000]={0};    cin>>n>>m;    for(int i=1;i<=n;i++)    cin>>date[i][0]>>date[i][1];    for(int i=1;i<=n;i++)    for(int j=m;j>=date[i][0];j--)    {        dp[j]=max(dp[j],dp[j-date[i][0]]+date[i][1]);    }    cout<<dp[m]<<endl;    return 0;}
0 0
原创粉丝点击