hdu--2159--二維費用背包<一維錯誤解法>

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   資料   for   

這題 還好 我A了之後 習慣性地去看了下 discuss  然後發現 基本上所有人的解法都在說 二維費用完全背包。。。

還好 看到一個人 是和我一樣的用 一維 完全背包 加一個計數的cnt數組去解決的。。。

還好 在那邊看到了一個人的質疑 並給出了一組資料  果然 一維的不能通過=-=那就只能 去學下 二維費用背包了...擦 這名字好長啊

因為 當時 我背包九講只學到了01 完全 多重 後面就沒學了 。。。

二維費用背包要是一般的話 很簡單的 就是多了一層for而已  而且假如這個數組是dp[ i ] [ u[i] ] [ v[i] ]那麼u[i] , v[i]的遍曆前後順序對面最終結果是沒有影響 只要你自己開的數組與這個前後遍曆順序相對應

      touch  me

我將 錯誤的代碼 也放上來好了。。。  雖然A了。只能怪 測試資料太弱了  同時 附上 測試資料

 1 /* 2 92 7 3 3 3 30 2 4 40 3 5 12 1 6 */ 7 #include <iostream> 8 #include <cstring> 9 using namespace std;10 11 const int size = 110;12 int dp[size];13 int cnt[size];14 int weight[size];15 int val[size];16 17 int main()18 {19     int n , m , k , s , ans;20     while( cin >> n >> m >> k >> s )21     {22         ans = -1;23         for( int i = 1 ; i<=k ; i++ )24         {25             cin >> val[i] >> weight[i];26         }27         memset( dp , 0 , sizeof(dp) );28         memset( cnt , 0 , sizeof(cnt) );29         for( int i = 1 ; i<=k ; i++ )30         {31             for( int j = weight[i] ; j<=m ; j++ )32             {33                 if( dp[ j-weight[i] ] + val[i] > dp[j] )34                 {35                     dp[j] = dp[ j-weight[i] ] + val[i];36                     cnt[j] = cnt[ j-weight[i] ] + 1;37                 }38             }39         }40         for( int i = 1 ; i<=m ; i++ )41         {42             if( dp[i]>=n && cnt[i]<=s )43             {44                 ans = m-i;45                 break;46             }47         }48         cout << ans << endl;49     }50     return 0;51 }
View Code

 

 1 // 二維 費用 背包 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6  7 const int size = 110; 8 int val[size]; 9 int weight[size];10 int dp[size][size];//一維 忍耐度  二維 殺怪數11 12 int main()13 {14     int n , m , k , s , ans;15     bool flag;16     while( cin >> n >> m >> k >> s )17     {18         flag = false;19         for( int i = 1 ; i<=k ; i++ )20         {21             cin >> val[i] >> weight[i];22         }23         memset( dp , 0 , sizeof(dp) );24         for( int i = 1 ; i<=k ; i++ )25         {26             for( int j = weight[i] ; j<=m ; j++ )27             {28                 for( int p =  1 ; p<=s ; p++ )29                 {30                     dp[j][p] = max( dp[j][p] , dp[ j-weight[i] ][ p-1 ] + val[i] );31                 }32             }33         }34         if( dp[m][s] <n )35             cout << -1 << endl;36         else37         {38             for( int i = 1 ; i<=m ; i++ )39             {40                 for( int j = 1 ; j<=s ; j++ )41                 {42                     if( dp[i][j]>=n )43                     {44                         ans = m-i;45                         flag = true;46                         break;47                     }48                 }49                 if( flag )50                     break;51             }52             cout << ans << endl;53         }54     }55     return 0;56 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.