1. 程式人生 > >Bone Collector-HDU

Bone Collector-HDU

vol total collector stream namespace ted this contest mod

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 ?

技術分享
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. Output One integer per line representing the maximum of the total value (this number will be less than 231
). Sample Input 1 5 10 1 2 3 4 5 5 4 3 2 1 Sample Output 14 Author Teddy Source HDU 1st “Vegetable-Birds Cup” Programming Open Contest 思路: 一維代碼:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
long long max(long
long num1,long long num2)//比較函數 { return num1>num2?num1:num2; } long long dp[1005];//定義背包數組 int main() { int N; int i,j,k; int bone[1005];//定義價值 int volume[1005];//定義容量 int t; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp));//初始化 int count,weight; scanf("%d%d",&count,&weight);//輸入組數和總重量 for(j=1;j<=count;j++) scanf("%d",&bone[j]); for(j=1;j<=count;j++) scanf("%d",&volume[j]); for(i=1;i<=count;i++) { for(k=weight;k>=0;k--) { if(k-volume[i]<0)//裝不下 { break; } else dp[k]=max(dp[k-volume[i]]+bone[i],dp[k]); cout << dp[k] << endl; } } printf("%lld\n",dp[weight]); } return 0; }

Bone Collector-HDU