比赛 2008haoi模拟训练2 评测结果 WWWWW
题目名称 公路建设 最终得分 0
用户昵称 梦里醉逍遥 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-04-23 10:07:36
显示代码纯文本
program road;
const
  fi='road.in';  fo='road.out';
type
  node=record
    g,w:longint;
  end;
  arr=array[1..500,1..500] of node;
  poi=array[1..500] of longint;
  anr=array[1..2000] of longint;
var
  fin,fout:text;
  n,m,r,p,rp:longint;  cost:real;
  graph:arr;  sp,stack,ts,hash:poi;

  procedure init;
  begin
    assign(fin,fi);  reset(fin);
    assign(fout,fo);  rewrite(fout);
    readln(fin,n,m);
  end;

  procedure init_dfs;
  begin
    fillchar(hash,sizeof(hash),0);
    p:=0;  rp:=0;
  end;

  procedure push(x:longint);
  begin
    p:=p+1;
    ts[p]:=x;
    hash[x]:=1;
  end;

  procedure pop;
  begin
    hash[ts[p]]:=0;
    p:=p-1;
  end;

  procedure dfs(i,j,x:longint);
  var
    k,l:longint;
  begin
    push(i);
    for k:=1 to sp[i] do
      if (graph[i,k].g<>0) and (graph[i,k].g<>x) then begin
        if graph[i,k].g=j then begin
          for l:=1 to p do stack[l]:=ts[l];
          p:=p+1;  stack[p]:=j;  rp:=p;
          exit;
        end;
        if hash[graph[i,k].g]=0 then
          dfs(graph[i,k].g,j,i);
        if rp<>0 then exit;
      end;
    pop;
  end;

  function reach(i,j:longint):boolean;
  begin
    init_dfs;
    dfs(i,j,0);
    if rp<>0 then reach:=true
    else reach:=false;
  end;

  procedure add(i,j,k:longint);
  var
    l:longint;
  begin
    for l:=1 to sp[i] do
      if graph[i,l].g=j then break;
    if graph[i,l].g=j then begin
      if graph[i,l].w>k then begin
        cost:=cost-graph[i,l].w+k;
        graph[i,l].w:=k;
        for l:=1 to sp[j] do
          if graph[j,l].g=i then graph[j,l].w:=k;
      end;
      exit;
    end;
    inc(sp[i]);  inc(sp[j]);
    graph[i,sp[i]].g:=j;
    graph[i,sp[i]].w:=k;
    graph[j,sp[j]].g:=i;
    graph[j,sp[j]].w:=k;
    cost:=cost+k;
  end;

  procedure del(i,j:longint);
  var
    k:longint;
  begin
    for k:=1 to sp[i] do
      if graph[i,k].g=j then begin
        graph[i,k].g:=0;
        graph[i,k].w:=0;
        if k=sp[i] then dec(sp[i]);
      end;
  end;

  procedure delete(i,j:longint);
  var
    k,l,best,besti,bestj:longint;
  begin
    init_dfs;
    dfs(i,j,0);
    best:=0;
    for k:=1 to rp-1 do
      for l:=1 to sp[k] do
        if graph[stack[k],l].g=stack[k+1] then
          if graph[stack[k],l].w>best then begin
            besti:=stack[k];
            bestj:=graph[besti,l].g;
            best:=graph[besti,l].w;
          end;
    cost:=cost-best;
    del(besti,bestj);  del(bestj,besti);
  end;

  procedure addedge(i,j,k:longint);
  begin
    if r=0 then begin
      add(i,j,k);
      r:=r+1;
      exit;
    end;
    add(i,j,k);
    if reach(i,i) then
      delete(i,i)
    else r:=r+1;
  end;

  procedure main;
  var
    i,j,k,l,x:longint;
  begin
    for i:=1 to m do begin
      readln(fin,j,k,l);
      addedge(j,k,l);
      if r<n-1 then writeln(fout,0)
      else writeln(fout,(cost*0.5):0:1);
    end;
    close(fin);  close(fout);
  end;

begin
  init;
  main;
end.