首页 > 代码库 > poj 3624 Charm Bracelet

poj 3624 Charm Bracelet

题目链接:http://poj.org/problem?id=3624

 

思路:

    经典的0-1背包问题:

    分析如下:

 

代码:

#include <iostream>using namespace std;const int MAX_N = 12880 + 10;int dp[MAX_N], W[MAX_N], D[MAX_N];int Max( int a, int b ) { return a > b ? a : b; }int main(){    int n, m;    memset( dp, 0, sizeof(dp) );    cin >> n >> m;    for ( int i = 1; i <= n; ++i )        cin >> W[i] >> D[i];    for ( int i = 1; i <= n; ++i )        for ( int j = m; j - W[i] >= 0; --j )                dp[j] = Max( dp[j], dp[j-W[i]]+D[i]);    cout << dp[m] << endl;    return 0;}

 

poj 3624 Charm Bracelet