USACO 2.1 健康的荷斯坦奶牛 Healthy Holsteins

39 篇文章 0 订阅
19 篇文章 0 订阅

健康的荷斯坦奶牛

题目描述

农民JOHN以拥有世界上最健康的奶牛为傲。他知道每种饲料中所包含的牛所需的最低的维他命量是多少。请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少。

给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少。

维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解。

分析:dfs,每次从dep(当前点)+1开始扩展就不会超时,搜过的点标记不搜,顺便记录一下序号。

代码

const
  maxn=100;
var
  a,b,d,x:array[0..maxn] of longint;
  c:array[0..maxn,0..maxn] of longint;
  f:array[0..maxn] of boolean;
  i,j,n,m,ans,min:longint;


function check:boolean;
var
  i:longint;
begin
  check:=true;
  for i:=1 to n do
    if a[i]>b[i] then exit(false);
end;


procedure dfs(dep,s,tot:longint);
var
  i,j,p:longint;
begin
  if tot>ans then exit;
  if (tot=ans) and (s>min) then exit;
  if a[1]<=b[1] then if check then
    begin
      if tot=ans then
        if s<min then
          begin
            min:=s;
            ans:=tot;
            for i:=1 to tot do
              d[i]:=x[i];
          end;
      if tot<ans then
        begin
          ans:=tot;
          min:=s;
          for i:=1 to tot do
              d[i]:=x[i];
        end;
      exit;
    end;
  p:=0;
  for i:=dep+1 to m do
    if not f[i] then
      begin
        f[i]:=true;
        x[tot+1]:=i;
        for j:=1 to n do
          begin
            b[j]:=b[j]+c[i,j];
            p:=p+c[i,j];
          end;
        dfs(i,s+p,tot+1);
        f[i]:=false;
        for j:=1 to n do
          dec(b[j],c[i,j]);
      end;
end;


begin
  ans:=maxlongint;
  min:=maxlongint;
  readln(n);
  for i:=1 to n do
    read(a[i]);
  readln(m);
  for i:=1 to m do
    for j:=1 to n do
      read(c[i,j]);
  dfs(0,0,0);
  write(ans,' ');
  for i:=1 to ans do
    write(d[i],' ');
end.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值