• NOI2003 逃学的小孩


    这一题不会做啊……

    我觉得真要比赛的话我可能会随机上几万次,然后再用LCA求距离,更新最优值,等到快超时的时候输出答案……

    题解请看2007年陈瑜希论文

    代码:

     1 const maxn=400100;
     2 type node=record
     3      w,go,next:longint;
     4      end;
     5 var  e:array[0..maxn] of node;
     6      f:array[0..maxn,1..3] of int64;
     7      first,u:array[0..maxn] of longint;
     8      i,x,y,z,n,tot,m:longint;
     9      ans:int64;
    10 function max(x,y:int64):int64;
    11  begin
    12  if x>y then exit(x) else exit(y);
    13  end;
    14 function get(x,y,z:int64):int64;
    15  begin
    16  get:=x+2*y+z;
    17  end;
    18 procedure insert(x,y,z:longint);
    19  begin
    20  inc(tot);e[tot].go:=y;e[tot].w:=z;e[tot].next:=first[x];first[x]:=tot;
    21  end;
    22 procedure init;
    23  begin
    24  readln(n,m);
    25  for i:=1 to m do
    26   begin
    27   readln(x,y,z);
    28   insert(x,y,z);
    29   insert(y,x,z);
    30   end;
    31  end;
    32 procedure update(x,y:int64);
    33  begin
    34   if y>f[x,1] then
    35    begin
    36    f[x,3]:=f[x,2];
    37    f[x,2]:=f[x,1];
    38    f[x,1]:=y;
    39    end
    40  else
    41   if y>f[x,2] then
    42    begin
    43    f[x,3]:=f[x,2];
    44    f[x,2]:=y;
    45    end
    46  else
    47    f[x,3]:=max(f[x,3],y);
    48  end;
    49 procedure dfs1(x,fa:longint);
    50  var i,y:longint;
    51  begin
    52  i:=first[x];
    53  while i<>0 do
    54   begin
    55   y:=e[i].go;
    56   if y<>fa then
    57    begin
    58    dfs1(y,x);
    59    u[y]:=e[i].w;
    60    update(x,f[y,1]+u[y]);
    61    end;
    62   i:=e[i].next;
    63   end;
    64  end;
    65 procedure dfs2(x,fa:longint);
    66  var i,y:longint;
    67  begin
    68  if f[x,1]+u[x]=f[fa,1] then update(x,f[fa,2]+u[x])
    69  else update(x,f[fa,1]+u[x]);
    70  ans:=max(ans,get(f[x,1],f[x,2],f[x,3]));
    71  i:=first[x];
    72  while i<>0 do
    73   begin
    74   y:=e[i].go;
    75   if y<>fa then dfs2(y,x);
    76   i:=e[i].next;
    77   end;
    78  end;
    79 procedure main;
    80  begin
    81  ans:=0;
    82  dfs1(1,0);
    83  dfs2(1,0);
    84  writeln(ans);
    85  end;
    86 begin
    87  init;
    88  main;
    89 end.     
    View Code

     唉,这种神题,我什么时候才能自己想到呢?

  • 相关阅读:
    Expected an assignment or function call and instead saw an expression
    ES6 中Object 的动态Key
    dotnet ef 无法执行,因为找不到指定的命令或文件
    Vue中的vfor
    使用TS开发Vue项目引入json文件报错处理
    JavaScript 防抖和节流
    MacOs 10.15.6 install pyodbc Tank
    一维数组转二维数组
    根据条件过滤目录树(子级匹配也要保留父级)
    跨域测试
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3784481.html
Copyright © 2020-2023  润新知