书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

加上减去DFS解决USACO Healthy Holsteins


Description

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 the cows. Help Farmer John feed his 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.

Input
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.
Output

 

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 Input

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

Sample Output

2 1 3

//加上减去深搜法

 

#include <iostream>
#include <string.h>
using namespace std;

int m,n,t;
int v[30],vi[30][30]; //v记录目标, vi记录可用选择
int total,p[30],ni[30];
bool line[30];

bool check()
{
for (int i = 0; i < m; i++)
if(p[i] < v[i]) return false; //只要有一个不满足就不行
return true;
}

void search(int k,int depth)
{
if(check() && depth < total) //递归的结束控制
   {
total = depth;
t = 0;
for (int i = 0; i < n; i++)
if(line[i]) ni[t++] = i+1; //记录行号
return;
}
for (int i = k+1;i < n; i++)
{
for (int j = 0; j < m; j++)
p[j]+=vi[i][j]; //再增加一排进行搜索
line[i] = true;
search(i,depth+1); //对应的depth要加1.
for (int j = 0; j < m; j++)
p[j]-=vi[i][j]; //将p[j]复原
line[i] = false;
}
}

int main()
{
cin >> m;
for (int i = 0; i < m; i++)
cin >> v[i];
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> vi[i][j];
total = 100;
memset(p,0,sizeof(p));
memset(line,false,sizeof(line));
for (int i = 0; i < n; i++) //i表示第i+1排
{
for (int j = 0; j < m; j++) //j表示第j+1列
p[j]+=vi[i][j]; //p[j]表示有i+1排时,第j列的和。
line[i] = true; //将第i排标记为正在使用中
search(i,1);
for (int j = 0; j < m; j++)
p[j]-=vi[i][j]; //将p[j]复原
line[i] = false;
}
cout << total <<" ";
t = 0;
for (int i = 0; i < total-1; i++)
   cout << ni[i] <<" ";
cout << ni[total-1] << endl;
return 0;
}



posted on 2011-10-23 14:47  More study needed.  阅读(417)  评论(0编辑  收藏  举报

导航

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!