3077. 【备战NOIP2012图论专项模拟试题】外星人入侵 (Standard IO)

来源:互联网 发布:sublime text 3运行js 编辑:程序博客网 时间:2024/05/02 03:01

Description

外星人入侵地球。可怕的吃人外星人正在全国各地依次序建立它们的基地。


全国共有N(1≤ 10,000)座城市,城市编号1N。城市之间有M(0≤ 100,000)条双向道路相连。外星人计划建立A(0AN)个基地。


你只有在距离当前所有外星人基地至少K(1K100)单位长度的城市才能得到安全。


所以你必须赶快写一个程序决定走到哪里去。


Input

1行:4个整数N, M, A, K


接下来M行,每行3个整数T1, T2(1T1<T2N)D(1D100),表示城市T1T2之间有一条长度为D的道路。两个城市之间最多有一条直连道路。


接下来A行,每行1个整数Bi(1BiN),表示外星人依次序建的第i个基地所在的城市编号。


Output

A行,第i1个整数,表示当外星人建好第i个基地后,距离当前所有基地B1,B2,...,Bi至少K长度的城市的数量。


Sample Input

7 6 3 31 2 11 3 12 5 13 6 11 4 14 7 2214

Sample Output

210

Data Constraint

Hint



题解

spfa,每建一个城市,算出其他点到它的最短路,因为城市会越来越少,所以为0是以后都是0。

代码

type
  arr=record
    x,y,w,next:int64;
  end;
var
  n,m,nm,nn,mm,ans:longint;
  a:array [0..400000] of arr;
  list,d,v,ls:array [0..400000] of int64;
  f:array [0..400000] of boolean;
procedure spfa(s:longint);
var
  i,k,h,t:longint;
begin
  fillchar(d,sizeof(d),63);
  h:=0;t:=1;
  v[s]:=1;list[1]:=s;d[s]:=0;
  repeat
    h:=h+1;
    i:=ls[list[h]];
    while i<>0 do
      begin
        if d[a[i].x]+a[i].w<d[a[i].y] then
          begin
            d[a[i].y]:=d[a[i].x]+a[i].w;
            if v[a[i].y]=0 then
              begin
                t:=t+1;
                list[t]:=a[i].y;
                v[a[i].y]:=1;
              end;
          end;
        i:=a[i].next;
      end;
    v[list[h]]:=0;
  until h=t;
end;
var
  i,j,t,sum:longint;
begin
  readln(n,m,nn,mm);
  for i:=1 to m do
    begin
      with a[i] do
        begin
          readln(x,y,w);
          next:=ls[x];
          ls[x]:=i;
          a[m+i].x:=y;
          a[m+i].y:=x;
          a[m+i].w:=w;
          a[m+i].next:=ls[a[m+i].x];
          ls[a[m+i].x]:=i+m;
        end;
    end;
  sum:=n;
  for i:=1 to nn do
    begin
      readln(t);
 if sum=0 then begin writeln(0);continue;end;
      spfa(t);
      ans:=0;
      for j:=1 to n do
        if (d[j]>=mm) and (f[j]=false) then inc(ans)
          else begin f[j]:=true;dec(sum);end;
      writeln(ans);
    end;
end.

0 0