健康的荷斯坦奶牛
题目描述
农民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.