Bone Collector
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 6
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 volume of V ,and along his trip of collecting there are a lot of
bones , obviously , different bone has different value and different volume, now
given the each bone’s value along his trip , can you calculate out the maximum
of the total value the bone collector can get ?
The bone collector had a big
bag with a volume of V ,and along his trip of collecting there are a lot of
bones , obviously , different bone has different value and different volume, now
given the each bone’s value along his trip , can you calculate out the maximum
of the total value the bone collector can get ?
Input
The first line contain a integer T , the number of
cases. Followed by T cases , each case three lines , the first line contain two
integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones
and the volume of his bag. And the second line contain N integers representing
the value of each bone. The third line contain N integers representing the
volume of each bone.
cases. Followed by T cases , each case three lines , the first line contain two
integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones
and the volume of his bag. And the second line contain N integers representing
the value of each bone. The third line contain N integers representing the
volume of each bone.
Output
One integer per line representing the maximum of the
total value (this number will be less than 2[sup]31[/sup]).
total value (this number will be less than 2[sup]31[/sup]).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
题解:也就01背包;
代码:
#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
int f[];
int val[];
int cos[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(f,,sizeof(f));
memset(val,,sizeof(val));
memset(cos,,sizeof(cos));
int n,v;
scanf("%d%d",&n,&v);
int i,j;
for(i=;i<=n;i++)
scanf("%d",&val[i]);
for(j=;j<=n;j++)
scanf("%d",&cos[j]);
for(i=;i<=n;i++)
{
for(j=v;j>=cos[i];j--)
{
// f[j]=f[j-1];
// if(j>=cos[i])
f[j]=max(f[j],f[j-cos[i]]+val[i]);
}
}
printf("%d\n",f[v]);
}
return ;
}
记忆化搜索;
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=;
int dp[MAXN][MAXN],w[MAXN],p[MAXN];
int dfs(int i,int v){
if(dp[i][v])return dp[i][v];
if(i==||v<)return ;//
if(w[i]>v)dp[i][v]=dfs(i-,v);
else dp[i][v]=max(dfs(i-,v),dfs(i-,v-w[i])+p[i]);//
return dp[i][v];
}
int main(){
int T,N,M;
SI(T);
while(T--){
SI(N);SI(M);
for(int i=;i<=N;i++)SI(p[i]);
for(int i=;i<=N;i++)SI(w[i]);
mem(dp,);
printf("%d\n",dfs(N,M));
}
return ;
}