您的位置:首页 > 其它

POJ-1837 Balance

2016-06-05 11:40 344 查看
题目大意:有一个天平,单臂长15。天平上有C个位置可以用来挂砝码。有G个砝码,求将所有砝码全部挂上时使天平平衡的方法有多少种。

题目链接:http://poj.org/problem?id=1837

注意题目中几个变量的范围都不大。暴力的话是O(C^G)复杂度,最坏为20^20,肯定不行。

寻找题目的递推规律,可以知道挂一个砝码时影响天平的状态,且只与挂之前的天平状态有关。使用二维dp数组做动态规划,第一维是当前挂的砝码数量,第二维是天平状态。天平状态使用合力矩来表示。合力矩范围为[-7500,7500],可平移到[0,15000]。

初始条件:

dp[0][7500]=1 其他均初始化为0

递推式:

dp[i][j+weight[i]*loc[k]]+=dp[i-1][j]

复杂度为C*G*15000

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stdlib.h>
#include <math.h>

using namespace std;

#define MAXN 21
#define MAXM 15001

int dp[MAXN][MAXM];
int loc[MAXN];
int weight[MAXN];

int main()
{
int c, g;
while (~scanf("%d%d", &c, &g))
{
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= c; i++)
scanf("%d", &loc[i]);
for (int i = 1; i <= g; i++)
scanf("%d", &weight[i]);
dp[0][7500] = 1;

for (int i = 1; i <= g; i++)
{
for (int j = 1; j <= 15000; j++)
{
for (int k = 1; k <= c; k++)
{
dp[i][j + weight[i] * loc[k]] += dp[i - 1][j];
}
}
}
printf("%d\n", dp[g][7500]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ