比赛 20110916 评测结果 AAAAAAAAAAA
题目名称 找工作 最终得分 100
用户昵称 wo shi 刘畅 运行时间 0.008 s
代码语言 Pascal 内存使用 9.04 MiB
提交时间 2011-09-16 21:37:13
显示代码纯文本
const
  oo=maxlongint;

var
  d,p,n,x,y,st,t,tot,time,i,j,z,f:longint;
  ans:int64;
  dis:array[0..10000]of int64;
  low,ds,bb:array[0..100000]of longint;
  q:array[0..1000000]of longint;
  v,s:array[0..10000]of boolean;
  g:array[0..1000,0..1000]of longint;

procedure print;
begin
  writeln(-1);
  close(input);
  close(output);
  halt;
end;

procedure spfa;
var
  h,t,i,x:longint;
begin
  h:=1;
  t:=1;
  for i:=1 to n do dis[i]:=maxlongint;
  for i:=1 to n do v[i]:=false;
  dis[st]:=-d;
  v[st]:=true;
  q[1]:=st;
  repeat
    x:=q[h];
    for i:=1 to n do
    if dis[x]+g[x,i]<dis[i] then
    begin
      dis[i]:=dis[x]+g[x,i];
      if not v[i] then
      begin
        v[i]:=true;
        inc(t);
        q[t]:=i;
      end;
      if t>100000 then print;
    end;
    inc(h);
    v[x]:=false;
  until (h>t);
end;

function min(x,y:longint):longint;
begin
  if x<y then exit(x);
  exit(y);
end;

procedure tarjan(u:longint);
var
  i,tot,v:longint;
  now:int64;
begin
  inc(time);
  ds[u]:=time;
  low[u]:=time;
  inc(t);
  q[t]:=u;
  s[u]:=true;
  for v:=1 to n do
  if g[u,v]<>oo then
  begin
    if (ds[v]=0) then
    begin
      tarjan(v);
      low[u]:=min(low[u],low[v]);
    end
    else if s[v] then
    low[u]:=min(low[u],ds[v]);
  end;
  if ds[u]=low[u] then
  begin
    tot:=0;
    repeat
      v:=q[t];
      inc(tot);
      bb[tot]:=v;
      s[v]:=false;
      dec(t);
    until u=v;
    if tot>1 then
    begin
      now:=0;
      for i:=1 to tot-1 do now:=now+g[bb[i+1],bb[i]];
      now:=now+g[bb[1],bb[tot]];
      if (now<0)and(dis[bb[1]]<>oo) then print;
    end;
  end;
end;

begin
  assign(input,'jobhunt.in'); reset(input);
  assign(output,'jobhunt.out'); rewrite(output);
  readln(d,p,n,f,st);
  for i:=1 to n do
   for j:=1 to n do
   g[i,j]:=oo;
  for i:=1 to p do
  begin
    readln(x,y);
    g[x,y]:=-d;
  end;
  for i:=1 to f do
  begin
    readln(x,y,z);
    if g[x,y]<>-d then g[x,y]:=z-d;
  end;
  spfa;
  time:=0;
  t:=0;
  for i:=1 to n do
  if ds[i]=0 then
  tarjan(i);
  ans:=maxlongint;
  for i:=1 to n do
  begin
    if dis[i]<ans then ans:=dis[i];
  end;
  writeln(-ans);
  close(input);
  close(output);
end.