比赛 20110414pm 评测结果 ATATTTTTTT
题目名称 龙凡 最终得分 20
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-04-14 17:28:19
显示代码纯文本
program travel;
const
  maxn=100010;
  maxm=400010;

type
  node=record
    v,w,next:longint;
  end;


var
  list,q,dist,dfn,low,dist1:array[0..maxn] of longint;
  map:array[0..maxm] of node;
  b,cut:array[0..maxn] of boolean;
  n,m,r1,r2,r3,h,t,i,j,i1,u,e,ans,temp,num:longint;


procedure addedge(i,j,w:longint);
begin
  inc(e);
  map[e].v:=j;
  map[e].w:=w;
  map[e].next:=list[i];
  list[i]:=e;
end;

function min(a,b:longint):longint;
begin
  if a<b then min:=a else min:=b;
end;


procedure dfs(i,fa:longint);
var
  j,u:longint;
begin
  inc(num);
  dfn[i]:=num;
  low[i]:=num;
  b[i]:=true;
  u:=list[i];
  while u<>0 do
  begin
    j:=map[u].v;
    if dfn[j]=0 then
    begin
      dfs(j,i);
      low[i]:=min(low[i],low[j]);
    end
    else
      if (b[j]) and (j<>fa)
        then low[i]:=min(low[i],dfn[j]);
    u:=map[u].next;
  end;
  if dfn[i]=low[i] then
  begin
    cut[i]:=true;
  end;
end;




begin
  assign(input,'travel.in');
  reset(input);
  assign(output,'travel.out');
  rewrite(output);

  readln(n,m);
  e:=0;
  for i:=1 to m do
  begin
    readln(r1,r2,r3);
    addedge(r1,r2,r3);
    addedge(r2,r1,r3);
  end;

  h:=0;
  t:=1;
  q[1]:=1;
  fillchar(b,sizeof(b),false);
  fillchar(dist,sizeof(dist),$FF);
  dist[1]:=0;
  b[1]:=true;
  while h<>t do
  begin
    h:=h+1;
    if h>n then h:=0;
    i:=q[h];
    u:=list[i];
    while u<>0 do
    begin
      j:=map[u].v;
      if (dist[i]+map[u].w<dist[j]) or (dist[j]=-1) then
      begin
        dist[j]:=dist[i]+map[u].w;
        if not b[j] then
        begin
          t:=t+1;
          if t>n then t:=0;
          q[t]:=j;
          b[j]:=true;
        end;
      end;
      u:=map[u].next;
    end;
    b[i]:=false;
  end;


  fillchar(cut,sizeof(cut),false);
  fillchar(b,sizeof(b),false);
  dfs(1,0);

  for i:=2 to n do
  begin
    if cut[i] then
    begin
      writeln(-1);
      continue;
    end;

    h:=0;
    t:=1;
    q[1]:=1;
    fillchar(b,sizeof(b),false);
    fillchar(dist1,sizeof(dist1),$FF);
    dist1[1]:=0;
    b[1]:=true;
    while h<>t do
    begin
      h:=h+1;
      if h>n then h:=0;
      i1:=q[h];
      u:=list[i1];
      while u<>0 do
      begin
        j:=map[u].v;
        if (j<>i) and ((dist1[i1]+map[u].w<dist1[j]) or (dist1[j]=-1)) then
        begin
          dist1[j]:=dist1[i1]+map[u].w;
          if not b[j] then
          begin
            t:=t+1;
            if t>n then t:=0;
            q[t]:=j;
            b[j]:=true;
          end;
        end;
        u:=map[u].next;
      end;
      b[i1]:=false;
    end;

    ans:=maxlongint;
    u:=list[i];
    while u<>0 do
    begin
      j:=map[u].v;
      temp:=dist1[j]+map[u].w;
      if (temp>dist[i]) and (temp<ans)
        then ans:=temp;
      u:=map[u].next;
    end;
    if ans=maxlongint
      then writeln(-1)
      else writeln(ans);
  end;

  close(input);
  close(output);
end.