POJ 3256(SPFA)

内容目录

这题只能对每一个点查一遍……

有向图的话能用floyd,可是迫于时限用了SPFA。

Program aa;
const
   maxk=10000;
   maxn=10000;
   maxm=10000;
var
   k,n,m,i,j,l:longint;
   a:array[1..maxk] of longint;
   q:array[1..maxn] of longint;
   edge,next,head:array[1..maxm] of longint;
   size:longint;
   res,num,b:array[1..maxn] of boolean;

procedure add(u,v:longint);
begin
   inc(size);
   edge[size]:=v;
   next[size]:=head[u];
   head[u]:=size;
end;


procedure spfa;
var
   i,j,p,now,v:longint;
begin
   i:=1;j:=1;
   while (i<=j) do
   begin
      now:=q[i];
      p:=head[now];
      while p<>0 do
      begin
         v:=edge[p];
         if not(b[v]) then
         begin
            b[v]:=true;
            inc(j);
            q[j]:=v;
         end;



         p:=next[p];
      end;
      inc(i);
   end;
   for i:=1 to n do
      res[i]:=res[i] and b[i];

end;

begin
   size:=0;
   fillchar(head,sizeof(head),0);
   fillchar(edge,sizeof(edge),0);
   fillchar(next,sizeof(next),0);
   fillchar(b,sizeof(b),false);
   fillchar(res,sizeof(res),true);
   fillchar(num,sizeof(num),false);
   read(k,n,m);
   for i:=1 to k do read(a[i]);
   for i:=1 to m do
   begin
      read(j,l);
      add(j,l);
   end;

   for i:=1 to k do
      if not(num[a[i]]) then
      begin
         num[a[i]]:=true;
         q[1]:=a[i];
         fillchar(b,sizeof(b),false);
         b[q[1]]:=true;
         spfa;
      end;
   l:=0;
   for i:=1 to n do if res[i] then inc(l);
   writeln(l);



end.