poj3642 Charm Bracelet(0-1背包)

题目意思:

给出N,M,N表示有N个物品,M表示背包的容量,接着给出每个物品的体积和价值,求背包能够装在的最大价值。

http://poj.org/problem?id=3624

题目分析:

o-1背包问题,转化方程。dp[j]:表示容量为j的时候,背包的最大价值

dp[j]=max(dp[j],dp[j-w[i]]+d[i]);

AC代码:

#include<iostream>
#include<cstring>
using namespace std;
int dp[20000],w[20000],d[20000];
int main()
{
    int n,v;
    while(cin>>n>>v){
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++){
            cin>>w[i]>>d[i];
        }
        for(int i=0;i<n;i++){
            for(int j=v;j>=w[i];j--){
                dp[j]=max(dp[j],dp[j-w[i]]+d[i]);
            }
        }
        cout<<dp[v]<<endl;
    }
    return 0;
}
时间: 2024-10-24 03:35:45

poj3642 Charm Bracelet(0-1背包)的相关文章

Charm Bracelet(01背包)

Problem 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 

POJ3624 Charm Bracelet 【01背包】

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22621   Accepted: 10157 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 fro

POJ 3624 Charm Bracelet(01背包模板)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45191   Accepted: 19318 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 fro

POJ 3624 Charm Bracelet【01背包】

解题思路:直接套公式就能做的01背包, for(i=1;i<=n;i++) { for(v=w[i];v<=m;v++) f[i,v]=max(f[i,v],f[i-1,v-w[i]]+d[i]);//只想明白了可以用一维数组来存放包的价值,因为我们需要的只是包的最大价值,不用记录是第几个包的时候,有最大价值,然后v从w[i]到包的总容量循环不明白. } for(i=1;i<=n;i++) { for(v=m;v>=c[i];v--) //即最开始给定包的总容量(此时包是空的),循

bzoj1625 / P2871 [USACO07DEC]手链Charm Bracelet

P2871 [USACO07DEC]手链Charm Bracelet 裸01背包. 看到自己1年半前写的30分code.......菜的真实(捂脸) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define re register 5 using namespace std; 6 int max(int a,int b){return a>b?a:b;} 7 int n,m,f[12

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 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 fro

POJ 3624 Charm Bracelet (01背包)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26078   Accepted: 11726 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 fro

[再做01背包] POJ 3624 Charm Bracelet

接触动态规划的第一题是数塔问题,第二题就是01背包问题了. 当时看的懵懵懂懂,回过头来再看这道题还是非常简单的了. 用 dp[i][j] 表示取前i种物品,使它们总体积不超过j的最优取法取得的价值总和状态转移方程:dp[i][j] = max(dp[i-1][j],dp[i-1][j-cost[i]]+weight[i]) 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstri

POJ 3624 Charm Bracelet 背包题解

最简单的背包问题了,本题应该除了背包就一个考点了:不能开二维数组.我没开过二维,不过看数据是不可以的.太大了. 做法有两种改进省内存DP: 1 所谓的滚动数组 2 逆向填表 很久没做背包DP,突然觉得这种背包问题很简单了. 下面给出两种解法: 1 calBag()是滚动数组 2 calBag2()是逆向填表 #pragma once #include <stdio.h> #include <stdlib.h> #include <vector> using namesp