Cod sursa(job #82461)

Utilizator gurneySachelarie Bogdan gurney Data 6 septembrie 2007 23:47:09
Problema Critice Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.92 kb
program critice;
  const
    fin='critice.in';
    fout='critice.out';
    nmax=1010;
    mmax=20020;
    inf=maxlongint shr 1;
type
  int=array[1..nmax] of integer;
  lint=array[1..nmax] of longint;
  tlint=^lint;
  tinteger=^int;
var
  target,prev,start:tinteger;
  capa,flow:tlint;
  last,pred,q:tinteger;
  used:array[1..nmax] of boolean;
  delta:tlint;
  pop,push,m,ii,cur,i,j,k,n,x,y,c,nedge:longint;
  tflow:longint;
  critic:array[1..nmax] of boolean;
  num:longint;


procedure insert(x,y,c:longint);
  begin
    inc(nedge);
    start^[nedge]:=x;target^[nedge]:=y;
    capa^[nedge]:=c;flow^[nedge]:=0;
    prev^[nedge]:=last^[x];
    last^[x]:=nedge;
    inc(nedge);
    start^[nedge]:=y;target^[nedge]:=x;
    capa^[nedge]:=c;flow^[nedge]:=0;
    prev^[nedge]:=last^[y];
    last^[y]:=nedge;
  end;

function op(n:integer):integer;
  begin
    if n and 1=1 then
      op:=n+1
    else
      op:=n-1;
  end;

begin
  new(last);new(pred);new(q);new(delta);
  new(prev);new(start);new(target);new(capa);new(flow);
  assign(input,fin);
    reset(input);
    readln(n,m);
    nedge:=0;
    for i:=1 to m do
      begin
        readln(x,y,c);
        insert(x,y,c);
      end;
  close(input);
  assign(output,fout);
    rewrite(output);
    tflow:=0;
    while true do
      begin
        pop:=1;push:=2;
        q^[1]:=1;
        fillchar(used,sizeof(used),false);
        delta^[1]:=inf;
        used[1]:=true;
        cur:=1;
        while pop<push do
          begin
            x:=last^[q^[pop]];
            while x>0 do
              begin
                if (abs(flow^[x])<capa^[x]) then
                  if (not(used[target^[x]])) then
                    begin
                      used[target^[x]]:=true;
                      q^[push]:=target^[x];
                      inc(push);
                      pred^[target^[x]]:=x;
                      delta^[target^[x]]:=delta^[start^[x]];
                      if delta^[target^[x]]>capa^[x]-flow^[x] then
                        delta^[target^[x]]:=capa^[x]-flow^[x];
                      if used[n] then
                        break;
                    end;
                x:=prev^[x];
              end;
            inc(pop);
          end;
            if not(used[n]) then
              break;
            cur:=n;
            x:=delta^[n];
            inc(tflow,delta^[n]);
            repeat
              x:=pred^[cur];
              inc(flow^[x],delta^[n]);
              y:=op(x);
              flow^[y]:=flow^[y]-delta^[n];
              cur:=start^[x];
            until cur=1;
      end;
    fillchar(used,sizeof(used),false);
    q^[1]:=1;pop:=1;push:=2;used[1]:=true;
    critic[1]:=true;
    while pop<push do
      begin
        cur:=q^[pop];
        inc(pop);
        x:=last^[cur];
        while x>0 do
          begin
            if (abs(flow^[x])<capa^[x]) and (not(used[target^[x]])) then
              begin
                used[target^[x]]:=true;
                q^[push]:=target^[x];
                inc(push);
                critic[target^[x]]:=true;
              end;
            x:=prev^[x];
          end;
      end;
    fillchar(used,sizeof(used),false);
    q^[1]:=n;pop:=1;push:=2;used[n]:=true;
    while pop<push do
      begin
        cur:=q^[pop];
        inc(pop);
        x:=last^[cur];
        while x>0 do
          begin
            if (abs(flow^[x])<capa^[x]) and (not(used[target^[x]])) then
              begin
                used[target^[x]]:=true;
                q^[push]:=target^[x];
                inc(push);
              end;
            x:=prev^[x];
          end;
      end;
    num:=0;
    for i:=1 to nedge do
      if (critic[start^[i]])and (used[target^[i]]) then
        inc(num);
    writeln(num);
    for i:=1 to nedge do
      if (critic[start^[i]])and (used[target^[i]]) then
        writeln((i+1)shr 1);
  close(output);
end.