记录编号 24815 评测结果 AAAAAAAAAA
题目名称 [网络流24题] 搭配飞行员 最终得分 100
用户昵称 Gravatarreamb 是否通过 通过
代码语言 Pascal 运行时间 0.006 s
提交时间 2011-05-16 17:14:29 内存使用 3.95 MiB
显示代码纯文本
program dinic;
var
  j,w,t,a,b,c,m,n,i,min,ans:longint;
  team,q,stack,dist:array[0..1000]of longint;
  g:array[0..1000,0..1000]of longint;
  biaozhi:boolean;
procedure fc;
var
  i:longint;
begin
  for i:=1 to m+1 do
    dist[i]:=0;
  t:=0;
  w:=1;
  team[1]:=0;
  while t<w do
  begin
    t:=t+1;
    for i:=1 to m+1 do
      if (g[team[t],i]>0)and(dist[i]=0) then
      begin
        w:=w+1;
        team[w]:=i;
        dist[i]:=dist[team[t]]+1
      end;
  end
end;
procedure zg;
var
  i:longint;
begin
  t:=1;
  stack[1]:=0;
  for i:=0 to m do
    q[i]:=0;
  while t>0 do
  begin
    repeat
      biaozhi:=true;
      for i:=q[stack[t]]+1 to m+1 do
        if (g[stack[t],i]>0)and(dist[i]=dist[stack[t]]+1) then
        begin
          q[stack[t]]:=i;
          t:=t+1;
          stack[t]:=i;
          biaozhi:=false;
          break
        end;
      if biaozhi then
      begin
        q[stack[t]]:=0;
        t:=t-1
      end
    until (stack[t]=m+1)or(t=0);
    min:=maxlongint;
    j:=0;
    for i:=1 to t-1 do
      if g[stack[i],stack[i+1]]<min then
      begin
        min:=g[stack[i],stack[i+1]];
        j:=i
      end;
    for i:=1 to t-1 do
    begin
      g[stack[i],stack[i+1]]:=g[stack[i],stack[i+1]]-min;
      g[stack[i+1],stack[i]]:=g[stack[i+1],stack[i]]+min
    end;
    if min<>maxlongint then
      ans:=ans+min;
    for i:=j+1 to t do
      q[stack[i]]:=0;
    t:=j;
  end
end;
begin
  assign (input,'flyer.in');
  reset (input);
  assign (output,'flyer.out');
  rewrite (output);
    readln (m,n);
    repeat
      readln (a,b);
      if a<>-1 then
        g[a,b]:=maxlongint
    until a=0;
    for i:=1 to n do
      g[0,i]:=1;
    for i:=n+1 to m do
      g[i,m+1]:=1;
    fc;
    while dist[m+1]>0 do
    begin
      zg;
      fc
    end;
    writeln (ans);   
  close (input);
  close (output)
end.