加上减去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;
}



转载于:https://www.cnblogs.com/o8le/archive/2011/10/23/2221769.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值