程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 杭電 2602 Bone Collector(背包問題 )

杭電 2602 Bone Collector(背包問題 )

編輯:C++入門知識

杭電 2602 Bone Collector(背包問題 )


Bone Collector

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 29109 Accepted Submission(s): 11898

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 ?
\


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 這是 01 背包問題 01 背包問題用到了遞推的思想,並且用到了分治 將大問題轉化為能夠容易解決的,小問題,從小問題來得到答案的思想 大致思路如下: c[i-1][v]表示前i-1件物品恰放入一個質量為m的背包可以取到最大價值,若只考慮第i件物品的話,那麼有兩種情況,就是放,與不放,要是放的話,那麼問題就轉化為“將前n-1件物品放入剩下的容量為m-w[i]的背包中,此時獲得的最大價值就是c[i-1][m-w[i]]再加上最後放進去的第i件的物品所獲得價值p[i];要是不放的話,問題就變成:”將前n-1件物品放入容量為j的背包中,此時獲得的最大價值就是c[i-1][m];然後題上一般有物品的數量,及相應的價值,通過兩個for循環就好了;最後的最大價值就是c[n][m]; 代碼如下:
#include
#include
int dp[1010][1010],val[1010],vol[1010];
int main()
{
 int n,i,j,v,t;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d%d",&n,&v);
  for(i=1;i<=n;i++)
  scanf("%d",&val[i]);
  for(j=1;j<=n;j++)
  scanf("%d",&vol[j]);
  memset(dp,0,sizeof(dp));
  for(i=1;i<=n;i++)
  for(j=0;j<=v;j++)//題上有個陷阱,就是將沒有體積但是有價值的 骨頭考慮了進去
  {
   if(vol[i]<=j&&dp[i-1][j]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved