其实我想搞图论..<面无表情飘过..> 消极情绪弥漫ing..
题意:
给 n 个挂钩和 g 个砝码..
求让天平平衡的条件有多少种..
思路:
因为 n 的范围是0~20 g 的范围也是0 ~ 20
所以如果暴力就会超时..
就DP吧..符合01背包的要求..
有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。
差不多..吧~
然后就正常的循环遍历每一个点..
查找符合条件的状态..
找到后再状态转移..
把Tips:提上来..
初始条件 :dp[0][7500] = 1; // 当有0件物品 力臂为 7500<极端条件 20*15*25 ..范围少少多点..平衡负数的功能 以7500作为0> 的方法有 1 种..就是不放东西..
动态转移方程: if(dp[i-1][k] != 0)
for(j = 1; j <= n; ++j){
dp[i][k+c[j]*w[i]] += dp[i-1][k];
结果: dp[ g ][7500]..
Code:
#include <stdio.h>
#include <cstring>
#include <fstream>
using namespace std;
int dp[25][15001];
int main()
{
int i, j, k;
int n, g;
int w[25], c[25];
// freopen("e:\\acm\\mess\\stdin-stdout\\in.txt", "r", stdin);
// freopen("e:\\acm\\mess\\stdin-stdout\\out.txt", "w", stdout);
while(scanf("%d %d", &n, &g) != EOF)
{
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; ++i)
scanf("%d", &c[i]);
for(i = 1; i <= g; ++i)
scanf("%d", &w[i]);
dp[0][7500] = 1;
for(i = 1; i <= g; ++i)
for(k = 0; k <= 15000; ++k){
if(dp[i-1][k] != 0) ///if it is a performability programme
for(j = 1; j <= n; ++j){///from 1 to n.. all c can put in it
dp[i][k+c[j]*w[i]] += dp[i-1][k]; ///put in it and the amount will increase
}
}
printf("%d\n", dp[g][7500]);
}
return 0;
}
没心情吖.. 英文翻译果断是有问题的..大概能理解就好了..因为编译器中英文转换太麻烦了..我典型的中式英语吖..(T^T)在这里丢人了..