记录编号 39371 评测结果 AAAAAAAAAAAA
题目名称 聪明的推销员 最终得分 100
用户昵称 Gravatarczp 是否通过 通过
代码语言 Pascal 运行时间 0.096 s
提交时间 2012-07-09 17:23:15 内存使用 69.00 MiB
显示代码纯文本
var
 x,y:array[1..8888] of longint;
 b,c,f,st,sm,op:array[1..3333] of longint;
 o:array[1..3333] of boolean;
 a,e:array[1..3000,0..3000] of longint;
 i,j,m,n,q,r,jh,l,ans:longint;
procedure dfs3(i:longint);
var j:longint;
begin
 o[i]:=true;
 for j:=1 to a[i,0] do
   if not o[a[i,j]] then dfs3(a[i,j]);
end;
procedure  dfs1(i:longint);
var j:longint;
begin
 o[i]:=true;
 for j:=1 to a[i,0] do
   if not o[a[i,j]] then dfs1(a[i,j]);
 inc(l);st[l]:=i;
end;
procedure  dfs2(i:longint);
var j:longint;
begin
 o[i]:=true;
 for j:=1 to e[i,0] do
   if not o[e[i,j]] then dfs2(e[i,j]);
 f[i]:=jh;
end;
begin
  assign(input,'salenet.in');reset(input);
  assign(output,'salenet.out');rewrite(output);
  readln(n);
  readln(q);
  for i:=1 to q do
   readln(b[i],c[i]);
  readln(r);
  for i:=1 to r do
   begin
    readln(x[i],y[i]);
    inc(a[x[i],0]);
    a[x[i],a[x[i],0]]:=y[i];
    inc(e[y[i],0]);
    e[y[i],e[y[i],0]]:=x[i];
   end;
   for i:=1 to q do if not o[b[i]] then dfs3(b[i]);
   for i:=1 to n do
   if not o[i] then begin
    writeln('NO');
    writeln(i);
    close(input);close(output);
    halt;
    end;
  fillchar(o,sizeof(o),0);
  for i:=1 to n do
   if not o[i] then dfs1(i);
  fillchar(o,sizeof(o),0);
  for i:=n downto 1 do
  if not o[st[i]] then
   begin
    inc(jh);
    dfs2(st[i]);
   end;
  for i:=1 to r do
   if f[x[i]]<>f[y[i]] then  inc(op[f[y[i]]]);
  fillchar(sm,sizeof(sm),$7f);
  for i:=1 to q do
    if sm[f[b[i]]]>c[i] then  sm[f[b[i]]]:=c[i];
  for i:=1 to jh do if  op[i]=0 then inc(ans,sm[i]);
  writeln('YES');
  writeln(ans);
  close(input);close(output);
end.