高中纪中OJ3077. 【备战NOIP2012图论专项模拟试题】外星人入侵 题解

来源:互联网 发布:中登公司 网络投票 编辑:程序博客网 时间:2024/05/01 21:44
problem:

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

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

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

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

input:

7 6 3 31 2 11 3 12 5 13 6 11 4 14 7 2214
output:
210
思路:进行a次spfa,不过dis和exist数组都不用清空,只用判断进行i次spfa后有哪几个dis[j]是不小于k的
伪代码:
   for l:=1 to n1 do        begin                que[1]:=a[l];                exist[a[l]]:=true;                head:=0;                tail:=1;                dis[a[l]]:=0;                while (head<>tail)do                begin                        head:=head mod 200000+1;                        x:=que[head];                        i:=last[x];                        while (i<>0) do                        begin                                y:=tov[i];                                if (dis[y]>dis[x]+len[i])then                                begin                                        dis[y]:=dis[x]+len[i];                                        if (not exist[y])then                                        begin                                                tail:=tail mod 200000+1;                                                que[tail]:=y;                                                exist[y]:=true;                                        end;                                end;                                i:=next[i];                        end;                        exist[x]:=false;                end;                zx:=0;                for j:=1 to n do                begin                        if dis[j]>=k then inc(zx);                end;                writeln(zx);        end;//spfa

0 0
原创粉丝点击