比赛 20120708 评测结果 C
题目名称 最小最大生成树 最终得分 0
用户昵称 SnowDancer 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-08 11:45:23
显示代码纯文本
program mstmn;
type
  edge=record
    v,e,next:longint;
  end;
  flowedge=record
    v:longint;
    c:boolean;
    next,against:longint;
  end;
var
  n,i,j,k,l,m,s,t,value,top,flowtop,maxflow,minE,minH,p:longint;
  edges:array[1..400008] of edge;
  flowedges:array[1..400008] of flowedge;
  now,height:array[1..20008] of longint;
  gap:array[0..20008] of longint;
  h:array[1..20008] of longint;
  flowh:array[1..20008] of longint;
  visit,can:array[1..20008] of boolean;
  find:boolean;
procedure insertE(u,v,e:longint);
  begin
    edges[top].v:=v;edges[top].e:=e;
    edges[top].next:=h[u]; h[u]:=top;
    inc(top);
  end;
procedure insertflowE(u,v,against:longint;c:boolean);
  begin
    flowedges[flowtop].v:=v;flowedges[flowtop].against:=against;
    flowedges[flowtop].next:=flowh[u]; flowh[u]:=flowtop;
    flowedges[flowtop].c:=c; inc(flowtop);
  end;
procedure DFS(u:longint;flag:boolean);
  var
    p:longint;
    x:edge;
  begin
    if u=t then exit;
    visit[u]:=true;p:=h[u];
    while p<>0 do begin
      x:=edges[p];
      if (not visit[x.v]) and (flag=(x.e<value)) and (x.e<>value) then begin
        DFS(x.v,flag);
        if can[x.v] then begin
          insertflowE(u,x.v,flowtop+1,true);
          insertflowE(x.v,u,flowtop-1,false);
          can[u]:=true;
        end;
      end;
      p:=x.next;
    end;
  end;
procedure SAP(k:longint);
  var
    x:flowedge;
  begin
    if k=t then begin
      find:=true; inc(maxflow);
      exit;
    end;
    while now[k]<>0 do begin
      x:=flowedges[now[k]];
      if (height[x.v]+1=height[k]) and (x.c) then begin
        SAP(x.v);
        if height[s]=n then exit;
        if find then break;
      end;
      now[k]:=x.next;
    end;
    if find then begin
      flowedges[now[k]].c:=false;
      flowedges[x.against].c:=true;
    end else begin
      minh:=n-1; p:=flowh[k]; minE:=p;
      while p<>0 do begin
        x:=flowedges[p];
        if (height[x.v]<minh) and x.c then begin
          minh:=height[x.v]; minE:=p;
        end;
        p:=x.next;
      end;
      dec(gap[height[k]]);
      if gap[height[k]]=0 then height[s]:=n;
      inc(gap[minh+1]);
      height[k]:=minh+1; now[k]:=minE;
    end;
  end;
begin
  //  OpenFile
  assign(input,'mstmn.in');reset(input);
  assign(output,'mstmn.out'); rewrite(output);
  //  Input
  readln(n,m);
  top:=1;
  for l:=1 to m do begin
    readln(i,j,k);
    insertE(i,j,k);
    insertE(j,i,k);
  end;
  readln(s,t,value);
  //  CalMinST
  flowtop:=1; can[t]:=true;
  DFS(s,true);
  gap[0]:=n; now:=flowh;
  repeat
    find:=false; SAP(s);
  until height[s]=n;
  //  CalMaxST
  fillchar(can,sizeof(can),false);
  fillchar(visit,sizeof(visit),false);
  fillchar(flowh,sizeof(flowh),0);
  fillchar(height,sizeof(height),0);
  fillchar(gap,sizeof(gap),0);
  flowtop:=1; can[t]:=true;
  DFS(s,false);
  gap[0]:=n; now:=flowh;
  repeat
    find:=false; SAP(s);
  until height[s]=n;
  //  Print
  writeln(maxflow);
  //  CloseFile
  close(input); close(output);
end.