POJ1837 Balance(DP)

POJ1837http://poj.org/problem?id=1837

题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数。

转化一下:DP[i][j]表示第i个物品挂上之后偏移量为j的方法数,所以最后结果就是DP[C][0]。下标不能有负数,所以整体往右移动7500即可。

 

 

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 #define MAX(a,b) (a > b ? a : b)
17 #define MIN(a,b) (a < b ? a : b)
18 #define mem0(a) memset(a,0,sizeof(a))
19 
20 typedef long long LL;
21 const double eps = 1e-12;
22 const int MAXN = 1005;
23 const int MAXM = 5005;
24 
25 int DP[22][15000], x[30], w[30];
26 int C, G;
27 
28 int main()
29 {
30     while(~scanf("%d%d", &C, &G))
31     {
32         mem0(DP);
33         for(int i=1;i<=C;i++) scanf("%d", &x[i]);
34         for(int i=1;i<=G;i++) scanf("%d", &w[i]);
35         DP[0][7500] = 1;
36         for(int i=1;i<=G;i++)
37         {
38             for(int j=0;j<15000;j++)
39             {
40                 if(DP[i-1][j])
41                 {
42                     for(int k=1;k<=C;k++)
43                     {
44                         DP[i][j+x[k]*w[i]] += DP[i-1][j];
45                     }
46                 }
47             }
48         }
49         printf("%d\n", DP[G][7500]);
50     }
51     return 0;
52 }

 

 

posted @ 2013-11-24 21:05  再见~雨泉  阅读(320)  评论(0编辑  收藏  举报