HDU2602 (0-1背包)

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39259????Accepted Submission(s): 16261


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
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2602
題意:給出n件物品的重量和價值,放進一個容量為v的背包,使背包里的價值最大。
0-1背包的模板題,可以有兩種解法。
一維數組:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int  w[1100],p[1110];
int f[1110];
int main() 
{int t,n,v;scanf("%d",&t);    while(t--){scanf("%d%d",&n,&v);for(int i=1;i<=n;i++)scanf("%d",&w[i]); //輸入物品重量 for(int i=1;i<=n;i++)scanf("%d",&p[i]); //輸入物品價值       memset(f,0,sizeof(f));for(int i=1;i<=n;i++)  {for(int j=v;j>=w[i];j--) //這個循環保證了放進去的物品重量不會超過背包所能容納的重量 
            {if(f[j] < f[j-w[i]] + p[i])  // 如果當前所擁有價值  小于 加上這件物品時創造的價值就更新 f[j]= f[j-w[i]] + p[i]; //   f[j] 表示背包重量為 j 時背包里的最大價值,//  所以f[ j - w[i] ] 表示放進這件物品時的狀態(因為放進該件物品后容量就減少了)
            }}    printf("%d\n",f[v]);}        return 0;
}

二維數組:

 1  #include<stdio.h>
 2  #include<string.h>
 3  #include<algorithm>
 4  using namespace std;
 5  int w[1100],p[1110];
 6  int f[1110][1110];
 7  int main()
 8  {
 9      int t,n,v;
10      scanf("%d",&t);    
11      while(t--)
12      {
13          scanf("%d%d",&n,&v);        
14          for(int i=1;i<=n;i++)
15          scanf("%d",&p[i]);
16          for(int i=1;i<=n;i++)
17          scanf("%d",&w[i]);        
18          memset(f,0,sizeof(f));                
19          for(int i=1;i<=n;i++)        
20          {
21              for(int j=0;j<=v;j++)
22              {
23                  if(w[i]<=j) // 這件物品的重量小于當前的容量,也就是說放的進背包 
24                  {
25                      // f[i][j] 表示第 i 件物品在背包容量為 j 時的狀態,
26                      //所以 f[i-1][j] 表示背包在上一次容量為 j 時候的狀態,也就是沒放這件物品的時候 
27                      f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+p[i]);// 比較 沒放進去之前 和放進該物品后 的價值,取最大 
28                            
29                  }
30                  else f[i][j]=f[i-1][j];  // 如果不能放進該物品,則取上一次的狀態  
31              }        
32          }
33          printf("%d\n",f[n][v]);                
34      }     
35      return 0;
36  }

?

渣渣一枚,如果有什么不對的地方,還請各位大神批評指正~ ?(^_^)

轉載于:https://www.cnblogs.com/ember/p/4701035.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375674.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375674.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375674.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

博客3萬訪問量了……

博客有3萬訪問量了呢。自從第一次用了贈送的1500的流量券&#xff0c;粉絲了從零突破了&#xff0c;到現在有150個粉絲了。 之前預想的寫博客的初衷&#xff0c;也是記錄自己的學習過程&#xff0c;畢竟好記憶不如爛筆頭&#xff0c;記錄下來就是長長久久的&#xff0c;隨時可以…

Mint17 一些安裝備忘

1&#xff0c;中文輸入法&#xff1a; sudo apt-add-repository ppa:fcitx-team/dailybuild-fcitx-master sudo apt-get update sudo apt-get upgrade sudo apt-get remove ^ibus* sudo apt-get remove ^fcitx* sudo apt-get install fcitx fcitx-bin fcitx-config-common fcit…

error: ‘CV_BGR2RGB’ was not declared in this scope

缺少庫&#xff0c;添加相應庫就行&#xff0c;ubuntu中Qt Create設置如下 LIBS -L/home/mima111/opencv345/lib -lopencv_imgcodecs -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_videoio 實際只要添加-lopencv_imgproc就行&#xff0c;CV_BGR2RGB變量存儲在該…

Struts學習之手動驗證

* 首先要從頁面中獲取對應的標簽name屬性的值&#xff0c;在動作類action中聲明同名的屬性&#xff0c;提供get和set方法 * 要繼承ActionSupport類或者實現Validateable接口 * 重寫Validateable接口的validate()方法 * 前提是&#xff1a;要保證setUsername()、va…

《啟示錄-打造用戶喜愛的產品》讀書小結

2014年大學畢業和研究生入學之間的暑假&#xff0c;我讀完了這本書。該書主要內容為介紹產品經理的一些工作經驗。分三方面內容系統介紹&#xff1a;人員、流程和產品。第一遍讀后&#xff0c;了解了一些產品經理的工作內容&#xff0c;也學習了很多優秀產品經理的理念。轉載于…

循環多少次?

循環多少次&#xff1f; Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 12 Problem Description我們知道&#xff0c;在編程中&#xff0c;我們時常需要考慮到時間復雜度&#xff0…

關于數據庫表的“記錄”與“字段”

何謂表的記錄&#xff1a; 就是數據庫中的一張表中的隨便任意一行稱之為記錄 何謂表的字段&#xff1a; 就是數據庫中的一張表中的隨便任意一列稱之為字段轉載于:https://www.cnblogs.com/cyh2009/p/4706021.html

error: use of deleted function

本文案例僅供參考 出錯的代碼如下&#xff1a; TEST(Test, test1) {TestImpl impl TestImpl(para1, para2);ASSERT_EQ("jkj", impl.func("22", "33", "44")); }實際應該這樣&#xff1a; TEST(Test, test1) {TestImpl impl(para1, …

WPF PasswordBox不支持綁定解決方法

PasswordBox的Password屬性因為安全原因不支持直接綁定&#xff0c;可以使用依賴屬性實現。直接插入代碼 public class PasswordBoxHelper{public static readonly DependencyProperty PasswordProperty DependencyProperty.RegisterAttached("Password",typeof(stri…

error: expected unqualified-id before 'public'

Error Coding class A{ }&#xff1b;class B public : A { };Correct Coding class A{ };class B : public A{ };

C# 方法返回值的個數

方法返回值類型總的來說分為值類型&#xff0c;引用類型,Void 有些方法顯示的標出返回值 public int Add(int a,int b) {return ab; } 有些方法隱式的返回返回值&#xff0c;我們可以將上面的方法改改&#xff1a; public void Add(int a,int b,out int sum) {sumab; } 怎么樣&…

【Java】Java里String 的equals和==

Java里面有對象和對象的引用的概念&#xff0c;在String方面&#xff0c;比較的是引用&#xff0c;equals比較的是對象的具體值。 String s1 new String("abc");String s2 new String("abc");System.out.println(s1 s2);System.out.println(s1.equals(s…

marked override, but does not override

檢查這個函數對應的基類函數 看是否是虛函數看函數參數是否對應

STL中的lower_bound和upper_bound的理解

STL迭代器表述范圍的時候&#xff0c;習慣用[a, b)&#xff0c;所以lower_bound表示的是第一個不小于給定元素的位置 upper_bound表示的是第一個大于給定元素的位置。 譬如&#xff0c;值val在容器內的時候&#xff0c;從lower_bound到 upper_bound表示的就是整個容器中與val相…

because the following virtual functions are pure within

構建了包含純虛函數的對象 包含純虛函數的類不能實例化為對象

C ~ 鏈式隊列與循環隊列

此處的鏈式與循環隊列可以應用于BFS和樹的層序遍歷。下面是對其結構和基本操作的程序描述。 1、循環隊列 解決循環隊列的隊空和隊滿的方法&#xff1a; [1].增加一個參數count&#xff0c;用來記錄數組中當前元素的個數&#xff1b; [2].為避免隊空和滿兩狀態混淆&#xff0c;少…

Hexo之部署github

最近開始學NodeJs&#xff0c;準備也在github上弄個一個Hexo博客練練過程中遇到一些問題總結一下。希望對遇到同樣問題的同學能有個幫助少走一些彎路。 - 其實用windows或mac客戶端直接去同步很順利沒遇到什么問題。主要是在使用Hexo deploy命令的時候。 我的配置文件deploy部分…

You have unstaged changes.

執行git rebase出錯 原因&#xff1a;有未提交的修改 解決&#xff1a;執行git stash暫時保存修改&#xff0c;rebase后&#xff0c;執行git stash pop

[bootstrap] 打造一個簡單的系統模板(1) 左側折疊菜單

1. 前言 最近需要做一個后臺管理系統&#xff0c;我打算使用bootstrap弄一個好看的后臺模板。網上的好多模板我覺的css和js有點重。 于是就打算完全依靠bootstrap搭建一個屬于自己的模板。 首先從左側的折疊菜單開始。看圖。 2. CSS 代碼 以下是自定義的css代碼&#xff0c;由于…

將gcc/g++鏈接到指定版本

安裝指定版本&#xff1a; sudo apt-get install gcc-4.8 sudo apt-get install g-4.8以上命令默認安裝的為4.8.5版本&#xff0c;支持c11 建立軟連接 cd /usr/bin如果已經裝有gcc或者g&#xff0c;需要先移除原先的軟連接sudo rm gcc sudo rm g建立新的軟連接 sudo ln -s g…