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

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

加上减去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   More study needed.  阅读(417)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2011年10月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

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

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

点击右上角即可分享
微信分享提示