poj 1837 01背包

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   os   io   

Balance

Time Limit: 1000 MS Memory Limit: 30000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

DescriptionGigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance.
It orders two arms of negligible weight and each arm‘s length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.

Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device.
It is guaranteed that will exist at least one solution for each test case at the evaluation.InputThe input has the following structure:
•the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20);
•the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are attached the device is balanced and lined up to the X axis; the absolute value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: ‘-‘ for the left arm and ‘+‘ for the right arm);
•on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights‘ values. OutputThe output contains the number M representing the number of possibilities to poise the balance. Sample Input
2 4-2 3 3 4 5 8
Sample Output
2
/*01背包題意:C個鉤碼(2—20) G個物品(2—20) 鉤碼位置(-25—25) 物品重量(0—20)  物品都用上且天平平衡有多少種方案 dp[i][j]:掛前i個物品達到狀態j 狀態j的取值範圍時-25*25*20——25*25*20  所以j取(-7500--7500) 防止出現負值 所以令j==15000  即j==7500時為平衡位置想~~每次掛砝碼都會影響天平的平衡 即狀態j  影響因素是力臂=c[i]*w[k] (n,m影響它的取值)         掛前i個物品時狀態是dp[i-1][j] 則掛第i個物品後狀態變為dp[i][j+c[i]*w[k]]       假設dp[i-1][j]的值是num  那麼  dp[i][j+c[i]*w[k]]也是num      即dp[i][j+c[i]*w[k]]+=dp[i-1][j]   前面狀態影響後面的    */ #include <iostream> #include <string.h> #include <stdio.h> int dp[35][15001]; ///前i個物品達到j的狀態有的dp[][]種 int main() {     int n,m;  ///鉤子個數 砝碼個數     int c[35]; ///鉤子的位置     int w[35]; ///砝碼重量     scanf("%d%d",&n,&m);     for(int i=1;i<=n;i++)     scanf("%d",&c[i]);     for(int j=1;j<=m;j++)     scanf("%d",&w[j]);     memset(dp,0,sizeof(dp));       dp[0][7500]=1;   ///因為防止出現負數情況 所以dp[][1500]了  同時dp[][7500]是平衡狀態     for(int i=1;i<=m;i++)       {         for(int j=0;j<=15000;j++)           {             for(int k=1;k<=n;k++)               {                 dp[i][j+c[k]*w[i]]+=dp[i-1][j]; ///核心  在前面介紹             }         }     }     printf("%d\n",dp[m][7500]); }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.