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--)  //即最开始给定包的总容量(此时包是空的),循环跳出条件是预留出现在正在放入的这个包的体积(这样你可以选择放入或者不放入),然后放下一个包的时候,之前包的总价值也被记录下来了。
		f[v]=max(f[v],f[v-w[i]]+d[i]);
}
#include<stdio.h>
#include<string.h>
int f[15000],w[3500],d[3500];
int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}
int main()
{
	int n,m;
	int i,v;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		memset(f,0,sizeof(f));
		for(i=1;i<=n;i++)
			scanf("%d %d",&w[i],&d[i]);
		for(i=1;i<=n;i++)
		{
			for(v=m;v>=w[i];v--)
			{
				f[v]=max(f[v],f[v-w[i]]+d[i]);
			}
		}
		printf("%d\n",f[m]);
	}
}
时间: 2024-08-03 11:14:48

POJ 3624 Charm Bracelet【01背包】的相关文章

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

POJ - 3624 Charm Bracelet (01背包基础)

题意: 即是给你一个容量M的包,有N件物品,每件物品有分别对应的 价值value 以及 重量weight .然后在不超过该背包容量的情况下能得到的最大价值为多少? 思路: 由于这是最基础的问题,所以就记录当对 01背包状态转移方程式的 理解. 对于动态规划来说,首先要知道我们要确定哪些状态量.然后再基于这些状态量进行状态转移得到我们最后希望得到的答案. 比如对于序列求最值来说我们习惯记录最后位取的是谁(即末位j),那么同理我们也可以记录我们当前选的是哪一种 物品.同时容量在状态中是必不可少的,所

[再做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)(背包入门)

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 

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

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> #