noip2014 寻找道路
来源:互联网 发布:淘宝助理怎么导出图片 编辑:程序博客网 时间:2024/06/06 14:10
const maxn=10010; maxm=200010;var nextt,too:array[0..2*maxm] of longint; x,y:array[0..maxm] of longint; head,q,step:array[0..maxn] of longint; vis:array[0..maxn] of boolean; n,m,s,t,i,tot:longint;procedure add(a,b:longint);begin inc(tot); nextt[tot]:=head[a]; too[tot]:=b; head[a]:=tot;end;function f(u:longint):boolean;var i:longint;begin i:=head[u]; while i>0 do begin if not vis[too[i]] then exit(false); i:=nextt[i]; end; exit(true);end;function bfs2:boolean;var l,r,u,i:longint;begin q[0]:=s; step[s]:=0; l:=0; r:=1; while l<r do begin u:=q[l]; inc(l); if not f(u) then continue; i:=head[u]; while i>0 do begin if step[too[i]]=-1 then begin step[too[i]]:=step[u]+1; q[r]:=too[i]; inc(r); if too[i]=t then begin writeln(step[too[i]]); exit(true); end; end; i:=nextt[i]; end; end; exit(false);end;procedure bfs1;var l,r,u,i:longint;begin q[0]:=t; vis[t]:=true; l:=0; r:=1; while l<r do begin u:=q[l]; inc(l); i:=head[u]; while i>0 do begin if not vis[too[i]] then begin vis[too[i]]:=true; q[r]:=too[i]; inc(r); end; i:=nextt[i]; end; end;end;begin tot:=0; fillchar(vis,sizeof(vis),false); fillchar(nextt,sizeof(nextt),0); fillchar(head,sizeof(head),0); fillchar(too,sizeof(too),0); readln(n,m); for i:=1 to m do begin readln(x[i],y[i]); add(y[i],x[i]); end; readln(s,t); bfs1; fillchar(nextt,sizeof(nextt),0); fillchar(head,sizeof(head),0); fillchar(too,sizeof(too),0); for i:=0 to maxn do step[i]:=-1; fillchar(q,sizeof(q),0); tot:=0; for i:=1 to m do add(x[i],y[i]); if not vis[s] then writeln(-1) else if not bfs2 then writeln(-1);end.
1 0
- Noip2014寻找道路题解
- NOIP2014 寻找道路
- [NOIP2014]寻找道路
- NOIP2014寻找道路
- vijos1909【noip2014】寻找道路
- 【noip2014】寻找道路
- NOIP2014 寻找道路
- noip2014寻找道路
- 【NOIP2014】D2T2 寻找道路
- 【noip2014】tyvj4058 寻找道路
- noip2014寻找道路
- noip2014 寻找道路
- [NOIP2014]寻找道路
- noip2014寻找道路
- [NOIP2014]寻找道路
- [NOIP2014][建图]寻找道路
- [noip2014]寻找道路 题解
- 【noip2014】寻找道路
- 深度学习主机配置 gtx1080 + cuda + tensorflow + ubuntu16
- POJ 2001 Shortest Prefixes
- IIS配置勾选
- Javascri表单验证
- Android蓝牙开发浅谈 __ 耳机录音
- noip2014 寻找道路
- 网站开发之鼠标悬停简单特效实现(四)
- Humble Numbers 【数论】【DP】
- zookeeper集群安装和简单命令学习
- Android Linker 与 SO 加壳技术
- 语音的基本概念
- 知识技能的培养策略——梯度化,差异化
- LeetCode 31. Next Permutation
- C语言连接数据库时出现的问题以及解决办法