1. 程式人生 > 其它 >2021-01-09 USACO Healthy Holsteins

2021-01-09 USACO Healthy Holsteins

技術標籤:C++學習# USACO

Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for his cows. Help Farmer John feed the cows so they stay healthy while minimizing the number of scoops that a cow is fed.

Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.

Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.

PROGRAM NAME: holstein

INPUT FORMAT

Line 1:integer V (1 <= V <= 25), the number of types of vitamins
Line 2:V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each day
Line 3:integer G (1 <= G <= 15), the number of types of feeds available
Lines 4..G+3:V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on.

SAMPLE INPUT (file holstein.in)

4
100 200 300 400
3
50   50  50  50
200 300 200 300
900 150 389 399

OUTPUT FORMAT

The output is a single line of output that contains:

  • the minimum number of scoops a cow must eat, followed by:
  • a SORTED list (from smallest to largest) of the feed types the cow is given

If more than one set of feedtypes yield a minimum of scoops, choose the set with the smallest feedtype numbers.

SAMPLE OUTPUT (file holstein.out)

2 1 3

無法接受這道題可以用暴力做出來。。。。虧我還想了各種dp。。。然後我打這個暴力打了兩天也是服氣自己。。。。我明白了,USACO這個階段的題就是在鍛鍊我們的打暴力水平!一定是這樣的。具體的做法就是每種scoop選還是不選,兩種方案,一共2^{15}種方案。直接搜尋就好了。

/*
ID: traysen1
TASK: holstein
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;

int V, G, ans_num = 1e9, ans_sta = 1e9;
int v[30], g[20][30];

bool check_sta(int sta) {
	int i, r, ans[30];
	memset(ans, 0, sizeof(ans));
	
	for (i = 1, r = 2; i <= G; i++, r <<= 1)
		if (r & sta)
			for (int j = 1; j <= V; j++) ans[j] += g[i][j];
	
	for (int j = 1; j <= V; j++)
		if (ans[j] < v[j]) return false;
	return true;
}

bool comp_sta(int a, int b) {
	int sta_1[20], sta_2[20];
	int total_1 = 0, total_2 = 0;
	
	int i, r;
	for (i = 1, r = 2; i <= G; i++, r <<= 1) {
		if (a & r) 
			sta_1[++total_1] = i;
		if (b & r) 
			sta_2[++total_2] = i;
	}
	
	for (int i = 1; i <= total_1; i++) {
		if (sta_1[i] < sta_2[i])
			return true;
		if (sta_1[i] > sta_2[i])
			return false;
	}
}

void dfs(int pos, int num, int sta) {
	if (pos > G) {
		if (check_sta(sta)) {
			if (num < ans_num) {
				ans_num = num;
				ans_sta = sta;
			} else if ((num == ans_num) && comp_sta(sta, ans_sta)) {
				ans_sta = sta;
			}
		}
		return;
	}
	
	dfs(pos + 1, num + 1, sta | (1 << pos));
	dfs(pos + 1, num, sta);
	return;
}

void output() {
	ofstream fout("holstein.out");
	fout << ans_num << " ";
	int i, r, total = 0, ans[30];
	for (i = 1, r = 2; i <= G; i++, r <<= 1)
		if (r & ans_sta) ans[++total] = i;
	for (int i = 1; i < total; i++)
		fout << ans[i] << " ";
	fout << ans[total] << endl;
	fout.close();
}

int main() {
	ifstream fin("holstein.in");
	fin >> V;
	for (int i = 1; i <= V; i++)
		fin >> v[i];
	fin >> G;
	for (int i = 1; i <= G; i++)
		for (int j = 1; j <= V; j++)
			fin >> g[i][j];
	fin.close();
	
	dfs(1, 0, 0);
	
	output();
	return 0;
}