比赛 20110412 评测结果 WWWWWWWWWW
题目名称 请客 最终得分 0
用户昵称 wo shi 刘畅 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-04-12 10:53:00
显示代码纯文本
uses math;

var
  ans,n,nn,i,j,m,head,tail:longint;
  s:array[0..10000]of longint;
  h:array[0..10000]of real;
  q,last,d,f:array[0..1000000]of longint;
  c:array[0..222,0..222]of longint;
  bool:array[0..2000,0..2000]of boolean;

procedure Init;
  var
       i,j,y,p,e,co,x:  longint;

  begin
    assign(input,'cookie.in'); reset(input);
    assign(output,'cookie.out'); rewrite(output);
    readln(n,m);
    nn:=n;
  for i:=1 to m do
  begin
    read(s[i+n+1]);
    y:=i+n+1;
    for j:=1 to s[y] do
    begin
      read(x);
      c[x+1,y]:=1;
      bool[x+1,y]:=true;
    end;
  end;

  for i:=2 to n+1 do
  begin
    for j:=n+2 to n+1+m do
    if c[i,j]=1 then
      h[i]:=h[i]+(1/s[j]);
    c[1,i]:=ceil(h[i]);
  end;

  for i:=n+2 to n+1+m do
  c[i,n+m+2]:=1;

  n:=n+m+2;
end;

procedure bfs;
var
  h,t,x,i:longint;
begin
  h:=1;
  t:=1;
  for i:=1 to n do d[i]:=-1;
  for i:=1 to n do f[i]:=0;
  q[1]:=1;
  d[1]:=0;
  repeat
    x:=q[h];
    for i:=1 to n do
    if (c[x,i]>0)and((d[i]=d[x]+1)or(d[i]=-1)) then
    begin
      if d[i]=-1 then
      begin
        inc(t);
        q[t]:=i;
      end;
      d[i]:=d[x]+1;
      inc(f[x]);
    end;
    inc(h);
  until h>t;
end;

procedure dfs;
var
  top,i,x,min:longint;
begin
  top:=1; q[top]:=1;
  for i:=1 to n do last[i]:=0;
  while top>0 do
  begin
    x:=q[top];
    if x<>n then
    begin
      if f[x]>0 then
      begin
        for i:=last[x]+1 to n do
        if (c[x,i]>0)and(d[i]=d[x]+1) then
        begin
          inc(top);
          q[top]:=i;
          last[x]:=i;
          dec(f[x]);
          break;
        end;
      end
      else begin
        q[top]:=0;
        dec(top);
      end;
    end
    else begin
      min:=maxlongint;
      for i:=1 to top-1 do
      if c[q[i],q[i+1]]<min then min:=c[q[i],q[i+1]];
      inc(ans,min);
      for i:=1 to top-1 do
      begin
        dec(c[q[i],q[i+1]],min);
        inc(c[q[i+1],q[i]],min);
      end;
      i:=1;
      while c[q[i],q[i+1]]>0 do inc(i);
      top:=i;
    end;
  end;
end;

begin
  init;
  repeat
    bfs;
    if d[n]=-1 then break;
    dfs;
  until false;
  if ans=m then
  begin
    for j:=nn+2 to n-1 do
    begin
     for i:=2 to nn+1 do
     if (c[i,j]=0)and(bool[i,j])and(c[j,n]=0) then
     begin
       writeln(i-1);
       break;
     end;
    end;
  end
  else writeln(-1);
  close(input);
  close(output);
end.