poj - 1837 - Balance(dp)
来源:互联网 发布:域名在哪买都是一样的 编辑:程序博客网 时间:2024/06/10 02:21
题意:一个天平,现要在其中的C(2 <= C <= 20)个位置(-15 <= x <= 15)挂G(2 <= G <= 20)个砝码(1 <= 单个质量 <= 25),问有多少种挂法使得天平平衡。
题目链接:http://poj.org/problem?id=1837
——>>状态:dp[i][j] 表示使用前 i 个砝码达到力矩和为 j 时的方案数。。
状态转移方程:dp[i][j] += dp[i - 1][j - w[i] * x[k]]。。
时间复杂度:O(C^2 * G^2)。。
#include <cstdio>#include <cstring>const int OFFSET = 7500;const int MAXN = 15000;const int MAXC = 20 + 5;const int MAXG = 20 + 5;int C, G;int x[MAXC];int w[MAXG];int dp[MAXG][MAXN + 10];void Read(){ for (int i = 1; i <= C; ++i) { scanf("%d", x + i); } for (int i = 1; i <= G; ++i) { scanf("%d", w + i); }}void Solve(){ memset(dp, 0, sizeof(dp)); dp[0][OFFSET] = 1; for (int i = 1; i <= G; ++i) { for (int k = 1; k <= C; ++k) { int wx = w[i] * x[k]; for (int j = 0; j <= MAXN; ++j) { if (j - wx >= 0) { dp[i][j] += dp[i - 1][j - wx]; } } } }}void Output(){ printf("%d\n", dp[G][OFFSET]);}int main(){ while (scanf("%d%d", &C, &G) == 2) { Read(); Solve(); Output(); } return 0;}
0 0
- POJ 1837 Balance(DP)
- POJ 1837 - Balance (dp)
- poj - 1837 - Balance(dp)
- POJ 1837 Balance(DP)
- POJ 1837 balance(dp)
- POJ 1837 Balance(DP)
- poj 1837 Balance(DP)
- POJ 1837 Balance(dp)
- POJ 1837 Balance DP
- POJ--1837--Balance--DP
- POJ 1837 Balance (DP)
- POJ 1837 Balance DP
- POJ 1837 Balance (DP)
- poj 1837 Balance (DP)
- POJ 1837 Balance DP
- POJ 1837 Balance (DP)
- POJ 1837 Balance (DP)
- poj 1837 Balance -DP
- 最长递增序列LIS研究
- 正则表达式匹配
- DSP2812--调试错误及解决办法1——Debug/led.out' not built
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- 第73题 Largest Number
- poj - 1837 - Balance(dp)
- php向数据库插入数据出现乱码问题
- ScrollView在初始化滚动条在某一位置的时候为什么非得开一个线程?还有就是scrollTo与smoothScrollTo的区别是什么?
- Xcode开发小技巧(快捷键)
- (2)Data-Part1. Definitions
- pantheon-terminal配置
- HALCON中的算子大全(中英对照)
- Java学习笔记(5)——Scanner类的使用
- 一些具有较强的可扩展性app的实现方法概述