USACO 6.3.3 Cowcycles dfs+剪枝

来源:互联网 发布:淘宝客服主管kpi考核 编辑:程序博客网 时间:2024/06/05 18:23
开始先搜索前齿轮的极值,然后搜索后齿轮的极值,这样就有条件算最大最小传动比率,然后筛选有效极值,最后再在最大值最小值之间做普通搜索,因为不符合要求的状态都被提前排除了(I.e.普通搜索中间不符合要求的状态没必要搜),总搜索次数也减小了。
做这题的收获是懂得了搜索时要尽量利用题目给出的条件进行优化,这题就是要缩小搜索区间。

代码:

{ID: ymwbegi1PROG: cowcycleLANG: PASCAL}var  i,j,k,l,f,r,a1,b1,c1,f1,f2,r1,r2:longint;  ans:real;  c:array[0..400] of real;  a,b,ansf,ansr:array[1..100] of longint;procedure sort;var  i,j:longint;begin  for i:=1 to c1-1 do    for j:=i+1 to c1 do      if c[i]>c[j] then      begin        c[0]:=c[i];c[i]:=c[j];c[j]:=c[0];      end;end;procedure dfs(x,y:longint);var  i,j:longint;  s,w:real;begin  if (x=f)and(y=r) then  begin    c1:=0;    for i:=1 to f do      for j:=1 to r do      begin        inc(c1);        c[c1]:=a[i]/b[j];      end;    sort;    s:=0;    for i:=2 to c1 do      s:=s+c[i]-c[i-1];    s:=s/(c1-1);    w:=0;    for i:=2 to c1 do      w:=w+sqr(c[i]-c[i-1]-s);    if w<ans then    begin      ans:=w;      ansf:=a;      ansr:=b;    end;    exit;  end;  if x<f    then for i:=a[x-1]+1 to a[f]-1 do         begin           a[x]:=i;           dfs(x+1,y);         end    else for i:=b[y-1]+1 to b[r]-1 do         begin           b[y]:=i;           dfs(x,y+1);         end;end;function min(x,y:longint):longint;begin  if x<y then exit(x)         else exit(y);end;begin  assign(input,'cowcycle.in');  assign(output,'cowcycle.out');  reset(input);  rewrite(output);  readln(f,r);  readln(f1,f2,r1,r2);  ans:=maxlongint;  for i:=f1 to f2-f+1 do    for j:=i+f-1 to f2 do      for k:=r1 to r2-r+1 do        for l:=k+r-1 to r2 do          if j*l>=3*i*k then          begin            a[1]:=i;            a[f]:=j;            b[1]:=k;            b[r]:=l;            dfs(min(f2-f1+1,2),min(r2-r1+1,2));          end;  for i:=1 to f do    if i>1      then write(' ',ansf[i])      else write(ansf[i]);  writeln;  for i:=1 to r do    if i>1      then write(' ',ansr[i])      else write(ansr[i]);  writeln;  close(input);  close(output);end.


0 0
原创粉丝点击