比赛 20120709 评测结果 AAAAAAAAAAAA
题目名称 聪明的推销员 最终得分 100
用户昵称 SnowDancer 运行时间 0.058 s
代码语言 Pascal 内存使用 34.59 MiB
提交时间 2012-07-09 11:09:24
显示代码纯文本
program salenet;
var
  n,i,j,k,l,index,top,ans,head,tail:longint;
  time,low,dfn,fa,stack,q:array[1..3000] of longint;
  link:array[1..3000,0..3000] of longint;
  indgree:array[1..3000] of longint;
  instack,visit:array[1..3000] of boolean;
  can:boolean;
procedure CalSCC(u:longint);
  var
    i:longint;
  begin
    inc(index); dfn[u]:=index; low[u]:=index;
    inc(top); stack[top]:=u; instack[u]:=true;
    for i:=1 to link[u,0] do begin
      if dfn[link[u,i]]=0 then begin
        CalSCC(link[u,i]);
        if low[link[u,i]]<low[u] then
          low[u]:=low[link[u,i]];
      end else
        if instack[link[u,i]] and (dfn[link[u,i]]<low[u]) then
          low[u]:=dfn[link[u,i]];
    end;
    if low[u]=dfn[u] then
      repeat
        k:=stack[top];
        instack[k]:=false;
        fa[k]:=u;
        if time[u]>time[k] then
          time[u]:=time[k];
        dec(top);
      until k=u;
  end;
begin
  //  OpenFile
  assign(input,'salenet.in'); reset(input);
  assign(output,'salenet.out'); rewrite(output);
  //  Input
  readln(n);
  for i:=1 to n do time[i]:=maxlongint;
  readln(l);
  for tail:=1 to l do begin
    readln(k,time[k]);
    q[tail]:=k; visit[k]:=true;
  end;
  readln(l);
  for k:=1 to l do begin
    readln(i,j);
    inc(link[i,0]);
    link[i,link[i,0]]:=j;
  end;
  //  Judge
  head:=0;
  repeat
    inc(head);
    for i:=1 to link[q[head],0] do
      if not visit[link[q[head],i]] then begin
        visit[link[q[head],i]]:=true;
        inc(tail); q[tail]:=link[q[head],i];
      end;
  until head=tail;
  for i:=1 to n do
    if not visit[i] then begin
      writeln('NO');writeln(i);
      //  Halt
      close(input); close(output);
      Halt;
    end;
  //  CalSCC
  for i:=1 to n do
    if dfn[i]=0 then
      CalSCC(i);
  //  CalIndgree
  for i:=1 to n do
    for j:=1 to link[i,0] do
      if fa[i]<>fa[link[i,j]] then
        inc(indgree[fa[link[i,j]]]);
  //  CalAns
  for i:=1 to n do
    if (fa[i]=i) and (indgree[i]=0) then
      inc(ans,time[i]);
  //  Print
  writeln('YES');
  writeln(ans);
  //  CloseFile
  close(input);close(output);
end.