UVA 10626 Buying Coke

大意略。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
using namespace std;

const int INF = 0x3f3f3f3f;

int d[160][160][60];

int cokes, num[3];

void read_case()
{
	memset(d, INF, sizeof(d));
	scanf("%d%d%d%d", &cokes, &num[0], &num[1], &num[2]);
}

void solve()
{
	read_case();
	int money1 = num[0] + 5*num[1] + 10*num[2];
	int money2 = money1 - 8*cokes;
	assert(money2 >= 0);
	for(int C = 0; C <= cokes; C++)
	{
		int money = money1 - 8*(cokes-C);
		for(int fives = 0; fives <= 150; fives++)
		{
			for(int tens = 0; tens <= num[2]; tens++)
			{
				int ones = money - 5*fives - 10*tens;
				if(ones < 0) continue;
				int &ans = d[C][fives][tens];
				if(C == 0) {ans = 0; continue;}
				if(tens >= 1)
				{
					ans = min(ans, d[C-1][fives][tens-1] + 1);
				}
				if(tens >= 1 && ones >= 3)
				{
					ans = min(ans, d[C-1][fives+1][tens-1] + 4);
				}
				if(ones >= 3 && fives >= 1)
				{
					ans = min(ans, d[C-1][fives-1][tens] + 4);
				}
				if(fives >= 2)
				{
					ans = min(ans, d[C-1][fives-2][tens] + 2);
				}
				if(ones >= 8)
				{
					ans = min(ans, d[C-1][fives][tens] + 8);
				}
			}
		}
	}
	printf("%d\n", d[cokes][num[1]][num[2]]);
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}


你可能感兴趣的:(UVA 10626 Buying Coke)