记录编号 39381 评测结果 AAAAAAAAAAAA
题目名称 聪明的推销员 最终得分 100
用户昵称 Gravatarisabella 是否通过 通过
代码语言 Pascal 运行时间 0.097 s
提交时间 2012-07-09 19:49:22 内存使用 68.92 MiB
显示代码纯文本
var
 v:array[1..3000]of boolean;
 pp,time,d,zhan,c,rec:array[1..3000]of longint;
 map,g:array[1..3000,0..3000]of longint;
 n,i,j,k,l,p,r,tot,mink,ans,top:longint;
 flag:boolean;
procedure floodfill(x:longint);
var i:longint;
begin
 v[x]:=true;
 for i:=1 to map[x,0] do
  if v[map[x,i]]=false then
   floodfill(map[x,i]);
end;

procedure dfs(x:longint);
var i:longint;
begin
 v[x]:=true;
 for i:=1 to map[x,0] do
  if v[map[x,i]]=false then dfs(map[x,i]);
 inc(tot);zhan[tot]:=x;
end;

procedure dfsg(x:longint);
var i:longint;
begin
 v[x]:=true;
 inc(top);rec[top]:=x;
 for i:=1 to g[x,0] do
  if v[g[x,i]]=false then dfsg(g[x,i]);
end;

begin
assign(input,'salenet.in');reset(input);
assign(output,'salenet.out');rewrite(output);
{init}
 fillchar(v,sizeof(v),0);
 fillchar(d,sizeof(d),0);
 readln(n);
 readln(p);
 for i:=1 to p do read(pp[i],time[pp[i]]);
 read(r);
 for i:=1 to r do
  begin
   read(j,k);inc(map[j,0]);map[j,map[j,0]]:=k;inc(d[k]);
   inc(g[k,0]);g[k,g[k,0]]:=j;
  end;
{judge 'no answer'}
 for i:=1 to p do
  if v[pp[i]]=false then floodfill(pp[i]);
 for i:=1 to n do if v[i]=false then
  begin writeln('NO');writeln(i);close(input);close(output);halt;end;
{shorten point:strong connection}
 fillchar(v,sizeof(v),0);
 tot:=0;
 for i:=1 to n do
  if v[i]=false then dfs(i);
 fillchar(v,sizeof(v),0);
 for i:=tot downto 1 do
  if v[zhan[i]]=false then
    begin
      top:=0;dfsg(zhan[i]);
      c:=d;mink:=maxlongint;
      for j:=1 to top do
       for k:=1 to map[rec[j],0]do dec(c[map[rec[j],k]]);
      flag:=true;
      for j:=1 to top do
       begin
        if (c[rec[j]]>0)then begin flag:=false;break;end;
        if (time[rec[j]]>0)and(time[rec[j]]<mink)then mink:=time[rec[j]];
       end;
      if (flag)then ans:=ans+mink;
    end;
 writeln('YES');
 writeln(ans);
close(input);close(output);
end.