POJ 3624 Charm Bracelet (基础01背包)

【题目链接】:click here~~

【思路】基础01背包

代码:

/*
* Problem: POJ No.3624
* Running time: 286MS
* Complier: C++
* Author: javaherongwei
* Create Time: 10:22 2015/9/5 星期六
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
const int N=12885;
int dp[N];
int value[N],weight[N];
int n,m;
int main()
{
    memset(dp,0,sizeof(dp));
    memset(value,0,sizeof(value));
    memset(weight,0,sizeof(weight));
    scanf("%d %d",&n,&m);
    for(int i=0; i<n; ++i) scanf("%d %d",&weight[i],&value[i]);
    for(int i=0; i<n; ++i)
    {
        for(int v=m; v>=weight[i]; --v)
        {
            dp[v]=max(dp[v],dp[v-weight[i]]+value[i]);
        }
    }
    printf("%d\n",dp[m]);
    return 0;
}

/*
Sample Input
4 6
1 4
2 6
3 12
2 7
Sample Output
23
*/


你可能感兴趣的:(dp,01背包)