记录编号 39378 评测结果 AAAAAAAAAAAA
题目名称 聪明的推销员 最终得分 100
用户昵称 Gravatarwo shi 刘畅 是否通过 通过
代码语言 Pascal 运行时间 0.225 s
提交时间 2012-07-09 18:52:48 内存使用 44.77 MiB
显示代码纯文本
var
  n,t,ans,tot,p,x,y,m,total,i,j:longint;
  s,v:array[0..10000]of boolean;
  ru,low,d:array[0..10000]of longint;
  fee,time,q,belong:array[0..100000]of longint;
  a,g:array[0..3000,0..3000]of integer;
  ch:array[0..3000,0..3000]of boolean;

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

procedure tarjan(x:longint);
var
  y,i:longint;
begin
  inc(tot);
  d[x]:=tot;
  low[x]:=tot;
  inc(t);
  q[t]:=x;
  s[x]:=true;
  for i:=1 to a[x,0] do
  begin
    y:=a[x,i];
    if d[y]=0 then
    begin
      tarjan(y);
      low[x]:=min(low[x],low[y]);
    end
    else if s[y] then low[x]:=min(low[x],d[y]);
  end;
  if d[x]=low[x] then
  begin
    inc(total);
    repeat
      y:=q[t];
      s[y]:=false;
      belong[y]:=total;
      fee[total]:=min(fee[total],time[y]);
      dec(t);
    until x=y;
  end;
end;

function yes:boolean;
var
  i:longint;
begin
  for i:=1 to total do
  if (ru[i]=0)and(fee[i]=maxlongint) then exit(false);
  exit(true);
end;

procedure go(x:longint);
var
  y,i:longint;
begin
  v[x]:=true;
  for i:=1 to g[x,0] do
  begin
    y:=g[x,i];
    if not v[y] then go(y);
  end;
end;

begin
  assign(input,'salenet.in'); reset(input);
  assign(output,'salenet.out'); rewrite(output);
  readln(n);
  readln(p);
  for i:=1 to n do
  begin
    time[i]:=maxlongint;
    fee[i]:=maxlongint;
  end;
  for i:=1 to p do
    readln(x,time[x]);
  readln(m);
  for i:=1 to m do
  begin
    readln(x,y);
    ch[x,y]:=true;
    inc(a[x,0]);
    a[x,a[x,0]]:=y;
  end;
  for i:=1 to n do
  if d[i]=0 then tarjan(i);
  for i:=1 to n do
   for j:=1 to n do
   if ch[i,j] then
   begin
     x:=belong[i];
     y:=belong[j];
     if x<>y then
     begin
       inc(g[x,0]);
       g[x,g[x,0]]:=y;
       inc(ru[y]);
     end;
   end;
  if yes then
  begin
    writeln('YES');
    ans:=0;
    for i:=1 to total do
    if ru[i]=0 then inc(ans,fee[i]);
    writeln(ans);
  end
  else begin
    writeln('NO');
    for i:=1 to total do
    if fee[i]<maxlongint then go(i);
    for i:=1 to n do
    if not v[belong[i]] then
    begin
      writeln(i);
      break;
    end;
  end;
  close(input);
  close(output);
end.