[DP] POJ - 3624 Charm Bracelet

Charm Bracelet
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 41212 Accepted: 17929

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.

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揹包問題,
輸入的第一行有兩個數,第一個是代表有n件物品,第二個表示揹包的重量限制。接下來有n行數據,每行數據包含兩個數字。第一個表示這件物品的重量,第二個數字表示這件物品的價值。
我們要做的是求出在重量限制下,揹包最多能裝多少價值的物品。每件物品只有一件。

//已AC代碼
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
#define N 1050017
int main()
{
    int dp[30000];
    int n,m;
    int w[4000];
    int d[4000];
    int i, j;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=1;i<=n;i++)
            cin>>w[i]>>d[i];
        memset(dp,0,sizeof(dp));
        for (i=1;i<=n;i++)
        {
            for (j=m;j>=w[i];j--)
            {
                dp[j]=max(dp[j],dp[j-w[i]]+d[i]);
            }
        }
        cout<<dp[m]<<endl;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章