Advertisement
Dennnhhhickk

Untitled

Jan 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. const
  9. MAXIN = 10000;
  10. INF = MAXINT - 1;
  11.  
  12. var
  13. n, m, i, j, x, y, ind, min, ans: integer;
  14. a: array [1..MAXIN, 1..MAXIN] of integer;
  15. d, mark: array [1..MAXIN] of integer;
  16. begin
  17. ans := 0;
  18. readln(n, m);
  19. for i := 1 to n do
  20. for j := 1 to n do
  21. a[i][j] := 0;
  22.  
  23. for i := 1 to m do
  24. begin
  25. readln(x, y);
  26. a[x][y] := 1;
  27. end;
  28.  
  29. for i := 1 to n do
  30. begin
  31. mark[i] := 0;
  32. d[i] := INF;
  33. end;
  34. d[1] := 0;
  35.  
  36. while (true) do
  37. begin
  38. ind := -1;
  39. min := INF;
  40. for i := 1 to n do
  41. if (mark[i] = 0) and (d[i] < min) then
  42. begin
  43. ind := i;
  44. min := d[i];
  45. end;
  46. if (ind = -1) then
  47. break;
  48. mark[ind] := 1;
  49. for i := 1 to n do
  50. if (a[ind][i] <> 0) and (mark[i] = 0) and (d[i] > d[ind] + 1) and (i <> n) then
  51. d[i] := d[ind] + 1
  52. else
  53. if (a[ind][i] <> 0) and (d[i] > d[ind] + 1) and (i = n) then
  54. begin
  55. d[i] := d[ind] + 1;
  56. ans := 1;
  57. end
  58. else
  59. if (i = n) and (a[ind][i] <> 0) and (d[i] = d[ind] + 1) then
  60. inc(ans);
  61. end;
  62.  
  63. writeln((d[n] - 1) * ans + 2);
  64. readln;
  65. readln;
  66. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement