2、Charm Bracelet( poj 3624)简单0-1背包

题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W【i】和魅力V【i】,问在不超过M的情况下能获得的魅力总和

思路:把M当背包总容量,用0-1背包写

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 60900
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
int dp[N],v[N],w[N];
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        met(v,0);
        met(w,0);
        for(int i=1; i<=n; i++)
            scanf("%d %d",&w[i],&v[i]);
        met(dp,0);
        for(int i=1; i<=n; i++)
        {
            for(int j=m; j>=w[i]; j--)
                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
        }
        printf("%d\n",dp[m]);
    }
    return 0;
}
时间: 2024-08-29 15:49:30

2、Charm Bracelet( poj 3624)简单0-1背包的相关文章

POJ 1745 【0/1 背包】

题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13431   Accepted: 4774 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequen

Charm Bracelet(poj3624)(01背包)

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

POJ 3624 Charm Bracelet

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 weightWi (1 ≤ Wi ≤

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

0-1背包问题,附上例题(POJ - 3624 Charm Bracelet)

0-1背包问题的题目样式 有 N 件物品和一个容量为 M 的背包.放入第 i 件物品耗费的费用是 Wi,得到的价值是 Vi.求解将哪些物品装入背包可使价值总和最大. 0-1背包问题关键在于该物品放或不放,即在当前容量为M的的情况下,选择不选择该物品,那么就有一个转移方程 for(i=0  -  N) for(j=0  -  M) dp[i][j] = max(dp[i-1][j],dp[i-1][j+w[i]]+v[i]); 当前物品为i,当前的背包容量为j,如果不选当前该物品,则选取dp[i-

poj 3624 Charm Bracelet 背包DP

Charm Bracelet Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 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 (

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

POJ 3624 Charm Bracelet (01)(背包入门)

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