AtCoder Beginner Contest 054

D - Mixing Experiment

题意:n个物品,给出一个比例a:b, 保证a, b互质, 要求你使用这n个物品配出a:b的比例的最小价值, 每个物品最多只能使用一次

很裸的01背包,比赛的时候愣是没看到每个物品只能使用一次

很烦

#include 
#include 
#include 
#include 
#include 

using namespace std;
#define LL long long
const int nmax = 40, abmax = 10, inf = 1000000;

int a[nmax], b[nmax], c[nmax];
int dp[nmax + 1][nmax * abmax + 1][nmax * abmax + 1];

int main(void){
	int n, ma, mb;
	cin >> n >> ma >> mb;
	for(int i = 0; i < n; ++i){
		cin >> a[i] >> b[i] >> c[i];
	}
	for(int i = 0; i <= n; ++i){
		for(int ca = 0; ca <= nmax * abmax; ++ca){
			for(int cb = 0; cb <= nmax * abmax; ++cb){
				dp[i][ca][cb] = inf;
			}
		}
	}
	dp[0][0][0] = 0;
	for(int i = 0; i < n; ++i){
		for(int ca = 0; ca <= nmax * abmax; ++ca){
			for(int cb = 0; cb <= nmax * abmax; ++cb){
				if(dp[i][ca][cb] == inf)	continue;
				dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb]);
				dp[i + 1][ca + a[i]][cb + b[i]] = min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i]);
			}
		}
	}
	int ans = inf;
	for(int ca = 1; ca <= nmax * abmax; ++ca){
		for(int cb = 1; cb <= nmax * abmax; ++cb){
			if(ca * mb == cb * ma)	ans = min(ans, dp[n][ca][cb]);
		}
	}
	if(ans == inf)	ans = -1;
	cout << ans << endl;
}



你可能感兴趣的:(Atcoder)