fork download
  1. uses math;
  2. const fi='';
  3. fo='';
  4. maxn=300;
  5. maxm=maxn*maxn;
  6. oo=trunc(1e9);
  7. type Tq =record
  8. u1,u2,k :longint;
  9. end;
  10. var q :array[1..2*maxn*maxn*10] of Tq;
  11. cx :array[1..maxn,1..maxn,1..2] of boolean;
  12. f :array[1..maxn,1..maxn,1..2] of longint;
  13. i,j,n,m,s,t :longint;
  14. dau,cuoi,res :longint;
  15. // danh sach
  16. link,head,ke :array[1..maxm] of longint;
  17. procedure enter;
  18. var u,v :longint;
  19. begin
  20. assign(input,fi);reset(input);
  21. readln(n,m,s,t);
  22. for i:=1 to m do
  23. begin
  24. read(u,v);
  25. link[i] := head[u];
  26. head[u] :=i;
  27. ke[i] := v;
  28. end;
  29. close(input);
  30. end;
  31. procedure push(x,y,z:longint);
  32. begin
  33. inc(cuoi);
  34. q[cuoi].u1:=x;
  35. q[cuoi].u2:=y;
  36. q[cuoi].k:=z;
  37. end;
  38. procedure process;
  39. var v1,v2,u1,u2,k :longint;
  40. begin
  41. dau :=1; cuoi :=0;
  42. push(s,t,1);
  43. fillchar(cx,sizeof(cx),true);
  44. //
  45. for i:=1 to n do
  46. for j:=1 to n do
  47. for k:=1 to 2 do
  48. f[i,j,k] := oo;
  49. f[s,t,1] := 0;
  50. while dau<=cuoi do
  51. begin
  52. u1 := q[dau].u1;
  53. u2 := q[dau].u2;
  54. k := q[dau].k;
  55. inc (dau);
  56. if k=1 then
  57. begin
  58. i := head[u1];
  59. while i>0 do
  60. begin
  61. v1 := ke[i];
  62. if cx[v1,u2,2] then
  63. begin
  64. f[v1,u2,2] := min(f[v1,u2,2],f[u1,u2,1]+1);
  65. push(v1,u2,2);
  66. cx[v1,u2,2] := false;
  67. end;
  68. i:= link[i];
  69. end;
  70. end;
  71. if k=2 then
  72. begin
  73. i := head[u2];
  74. while i>0 do
  75. begin
  76. v2 := ke[i];
  77. if cx[u1,v2,1] then
  78. begin
  79. f[u1,v2,1] := min(f[u1,u2,2]+1,f[u1,v2,1]);
  80. push(u1,v2,1);
  81. cx[u1,v2,1] := false;
  82. end;
  83. i:= link[i];
  84. end;
  85. end;
  86. end;
  87. res := oo;
  88. for i:=1 to n do
  89. res := min(res,f[i,i,1]);
  90. end;
  91. procedure print;
  92. begin
  93. assign(output,fo);rewrite(output);
  94. if res=oo then write(-1) else write(res div 2);
  95. close(output);
  96. end;
  97. begin
  98. enter;
  99. process;
  100. print;
  101. end.
Success #stdin #stdout 0s 23624KB
stdin
Standard input is empty
stdout
-1