比赛 2008haoi模拟训练2 评测结果 EEETT
题目名称 公路建设 最终得分 0
用户昵称 thegy 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-04-23 11:09:42
显示代码纯文本
program road;
var
  fin,fout:text;
  n,m,i,ii,j,a,b,d,e,f:longint;
  c,ans:real;
  v,vt:boolean;
  g:array[1..500,1..500]of real;
  vv:array[1..500]of boolean;
  baobian:array[1..500,1..500]of boolean;
procedure dfss(x,y:longint;var xx,yy:longint);
var
  i,head,tail,tot,x0,y0:longint;
  max:real;
  l,f,outit:array[1..500]of longint;
  v:array[1..500]of boolean;
begin
  for i:=1 to n do v[i]:=false;
  l[1]:=x;
  f[1]:=0;
  v[x]:=true;
  head:=1;
  tail:=1;
  repeat
    for i:=1 to n do
    begin
      if not(v[i]) and baobian[l[head],i] then
      begin
        inc(tail);
        l[tail]:=i;
        f[tail]:=head;
        v[i]:=true;
      end;
    end;
    inc(head);
  until (head>tail) or (l[head]=y);
  outit[1]:=tail;
  tot:=1;
  repeat
    y:=f[y];
    inc(tot);
    outit[tot]:=y;
  until f[y]=0;
  max:=0;
  for i:=1 to tot-1 do
  begin
    x0:=l[outit[i]];
    y0:=l[outit[i+1]];
    if g[x0,y0]>max then
    begin
      max:=g[x0,y0];
      xx:=x0;
      yy:=y0;
    end;
  end;
end;

procedure makeit;
var i,j,k,t,d,d2:longint;
    min:real;
    q:array[1..500] of integer;
    pand:array[1..500] of boolean;
begin
  ans:=0;
  for i:=1 to n do
  for j:=1 to n do
    baobian[i,j]:=false;
  t:=1;
  q[1]:=1;
  for i:=1 to n do pand[i]:=false;
  pand[1]:=true;
  for k:=1 to n-1 do
  begin
    min:=9999999;
    for i:=1 to t do
    for j:=1 to n do
    if not(pand[j]) then
      if g[q[i],j]<min then begin d:=j; d2:=i; min:=g[q[i],j]; end;
    inc(t); ans:=ans+min; q[t]:=d; pand[d]:=true; baobian[d,d2]:=true; baobian[d2,d]:=true;
  end;
end;

procedure dfs(x:longint);
var
  i:longint;
begin
  vv[x]:=true;
  for i:=1 to n do
  begin
    if vv[i] then continue;
    if g[x,i]<>9999999 then dfs(i);
  end;
end;
begin
  assign(fin,'road.in'); reset(fin);
  assign(fout,'road.out'); rewrite(fout);
  read(fin,n,m);
  for i:=1 to n do
  for j:=1 to n do g[i,j]:=9999999;
  v:=false;
  f:=0;
  for ii:=1 to m do
  begin
    read(fin,a,b,c);
    if not(v) then
    begin
      if c<g[a,b] then begin g[a,b]:=c; g[b,a]:=c; end;
      for i:=1 to n do vv[i]:=false;
      dfs(1);
      vt:=true;
      for i:=1 to n do
        if not(vv[i]) then begin vt:=false; break; end;
      v:=vt;
    end;
    if not(v) then begin writeln(fout,'0'); continue; end;
    if f=0 then
    begin
      f:=1;
      makeit; {qiu zhi || bao bian}
      writeln(fout,(ans/2):0:1);
      continue;
    end;
    if baobian[a,b] then
    begin
      if g[a,b]<c then writeln(fout,(ans/2):0:1)
      else
      begin
        ans:=ans-g[a,b]+c;
        g[a,b]:=c;
        g[b,a]:=c;
        writeln(fout,(ans/2):0:1);
      end;
    end
    else
    begin
      if g[a,b]<c then writeln(fout,(ans/2):0:1)
      else
      begin
        g[a,b]:=c;
        g[b,a]:=c;
        dfss(a,b,d,e);
        if g[d,e]<g[a,b] then writeln(fout,(ans/2):0:1)
        else
        begin
          baobian[d,e]:=false;
          baobian[e,d]:=false;
          baobian[a,b]:=true;
          baobian[b,a]:=true;
          ans:=ans+g[a,b]-g[d,e];
          writeln(fout,(ans/2):0:1);
        end;
      end;
    end;
  end;
  close(fout);
end.