杭电2602 Bone Collector 【01背包】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

解题思路:给出一个容量为V的包,以及n个物品,每一个物品的耗费的费用记作c[i](即该物品的体积),每一个物品的价值记作w[i],

我们用 f[v]来表示一个容量为v的包的总价值,这样最后我们只需要输出f[V]就能得出结果

则对于第i个物品,它可以放入背包,此时背包的容量变为v-c[i],背包的总价值变为f[v-c[i]]+w[i],

它也可以不放入背包,此时背包的容量还是v,背包的总价值不变,仍为f[v]

所以我们只需要取这两种情况中的最大值即可

f[v]=max(f(v),f(v-c[i])+w[i]);

#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}

int main()
{
	int ncase,n,V;
	int i,v;
	int c[1005],w[1005];
	long int f[1005];
	while(scanf("%d",&ncase)!=EOF)
	{
		while(ncase--)
		{
			for(i=0;i<1005;i++)
				f[i]=0;
			scanf("%d %d",&n,&V);
			for(i=0;i<n;i++)
				scanf("%d",&w[i]);  //每一个的价值
			for(i=0;i<n;i++)
				scanf("%d",&c[i]);//每一个的体积

			for(i=0;i<n;i++)
			{
				for(v=V;v>=c[i];v--)
				{
					f[v]=max(f[v],f[v-c[i]]+w[i]);
				}
			}
			printf("%ld\n",f[V]);
		}
	}
}

  

时间: 2024-10-13 02:20:30

杭电2602 Bone Collector 【01背包】的相关文章

杭电 2602 Bone Collector

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 27413    Accepted Submission(s): 11154 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

Bone Collector(杭电2602)(01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31604    Accepted Submission(s): 13005 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

HDU 2602 Bone Collector(01背包裸题)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 60469    Accepted Submission(s): 25209 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo

杭电2602 Bone Collector

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65099    Accepted Submission(s): 27122 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bon

hdu 2602 Bone Collector 01背包

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 50442    Accepted Submission(s): 21153 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon

HDU 2602 Bone Collector (01背包DP)

题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>

hdu 2602 Bone Collector 01背包模板

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目要求是尽量装的最多,所以初始化的时候都为0即可. 如果要求恰好装满,初始化的时候除了dp[0] = 0,其他都要设成-inf,表示不合法情况. 1 #include <bits/stdc++.h> 2 using namespace std; 3 int T; 4 int dp[1010][1010]; 5 int v[1010], w[1010]; 6 int main() 7 { 8

Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!详解!)

Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's , also he went to the grave - The bone collector had a big bag with a volu

hdu2602 Bone Collector (01背包)

本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //============================================================================ // Name : 2602.cpp // Author : vit // Version : // Copyright : Your copyright notice // Description : Hello