fork(5) download
  1. <?php
  2.  
  3. $paths = array(
  4. 'one' => array(
  5. 'visited' => false, // были ли в вершине: false = небыли, true = были
  6. 'shortest_path' => INF, // наименьшая длина пути до вершины INF = бесконечно.
  7. 'neighbors' => array( // масив с соседями вершины, их длинна и транспорт имя
  8. 'two' => array(1 => BUS),
  9. 'three' => array(5 => SUBWAY),
  10. 'fow' => array(10 => BUS),
  11. )
  12. ),
  13.  
  14. 'two' => array(
  15. 'visited' => false,
  16. 'shortest_path' => INF,
  17. 'neighbors' => array(
  18. 'one' => array(1 => BUS),
  19. )
  20. ),
  21.  
  22. 'three' => array(
  23. 'visited' => false,
  24. 'shortest_path' => INF,
  25. 'neighbors' => array(
  26. 'one' => array(5 => BUS),
  27. 'six' => array(20 => FOOT)
  28. )
  29. ),
  30.  
  31. 'fow' => array(
  32. 'visited' => false,
  33. 'shortest_path' => INF,
  34. 'neighbors' => array(
  35. 'one' => array(10 => BUS),
  36. 'five' => array(5 => BUS)
  37. )
  38. ),
  39.  
  40. 'five' => array(
  41. 'visited' => false,
  42. 'shortest_path' => INF,
  43. 'neighbors' => array(
  44. 'fow' => array(5 => BUS),
  45. 'six' => array(5 => FOOT)
  46. )
  47. ),
  48.  
  49. 'six' => array(
  50. 'visited' => false,
  51. 'shortest_path' => INF,
  52. 'neighbors' => array(
  53. 'three' => array(20 => BUS),
  54. 'five' => array(5 => FOOT)
  55. )
  56. )
  57. );
  58.  
  59. $start_point = one;
  60. $end_point = six;
  61.  
  62.  
  63. function asd($paths,$start_point,$end_point)
  64. {
  65. $paths[$start_point][shortest_path] = 0; // делаем длинну пути стартовой точки = 0
  66. while ($paths[$end_point][visited] == false) { // циклим пока точка до которой мы идем не станет true
  67. $paths[$start_point][visited] = true; // ставим посещение true
  68. foreach ($paths[$start_point][neighbors] as $name => $array_path_trnnames) { // смотрим соседей вершины и длинну пути и присваем if длина пути+пройденый путь меньше чем есть
  69. foreach ($array_path_trnnames as $path_length => $transport_name) {
  70. if ($paths[$name][visited] == false) {
  71. if ($path_length+$paths[$start_point][shortest_path]< $paths[$name][shortest_path]) {
  72. $paths[$name][shortest_path] = $path_length+$paths[$start_point][shortest_path];
  73. }
  74. }
  75. }
  76. }
  77. $j = INF;
  78. foreach ($paths as $name => $value) { // делаем старт поинт вершину с наименьшем пути
  79. if ($paths[$name][visited] == false ) {
  80. if ($paths[$name][shortest_path]< $j) {
  81. $j = $paths[$name][shortest_path];
  82. $start_point = $name;
  83. }
  84. }
  85. }
  86. $array[] = $start_point." \n"; // массив со всеми пройдеными точками
  87. }
  88. return $array;
  89. }
  90.  
  91. $array = asd($paths,$start_point,$end_point);
  92.  
  93. //var_export ($array) ;
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
Success #stdin #stdout #stderr 0s 82880KB
stdin
Standard input is empty
stdout
array (
  0 => 'two 
',
  1 => 'three 
',
  2 => 'fow 
',
  3 => 'five 
',
  4 => 'six 
',
  5 => 'six 
',
)
stderr
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 8
PHP Notice:  Use of undefined constant SUBWAY - assumed 'SUBWAY' in /home/05CCSO/prog.php on line 9
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 10
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 18
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 26
PHP Notice:  Use of undefined constant FOOT - assumed 'FOOT' in /home/05CCSO/prog.php on line 27
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 35
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 36
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 44
PHP Notice:  Use of undefined constant FOOT - assumed 'FOOT' in /home/05CCSO/prog.php on line 45
PHP Notice:  Use of undefined constant BUS - assumed 'BUS' in /home/05CCSO/prog.php on line 53
PHP Notice:  Use of undefined constant FOOT - assumed 'FOOT' in /home/05CCSO/prog.php on line 54
PHP Notice:  Use of undefined constant one - assumed 'one' in /home/05CCSO/prog.php on line 59
PHP Notice:  Use of undefined constant six - assumed 'six' in /home/05CCSO/prog.php on line 60
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 65
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 67
PHP Notice:  Use of undefined constant neighbors - assumed 'neighbors' in /home/05CCSO/prog.php on line 68
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 81
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 67
PHP Notice:  Use of undefined constant neighbors - assumed 'neighbors' in /home/05CCSO/prog.php on line 68
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 81
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 67
PHP Notice:  Use of undefined constant neighbors - assumed 'neighbors' in /home/05CCSO/prog.php on line 68
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 81
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 67
PHP Notice:  Use of undefined constant neighbors - assumed 'neighbors' in /home/05CCSO/prog.php on line 68
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 81
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 67
PHP Notice:  Use of undefined constant neighbors - assumed 'neighbors' in /home/05CCSO/prog.php on line 68
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 71
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 72
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 80
PHP Notice:  Use of undefined constant shortest_path - assumed 'shortest_path' in /home/05CCSO/prog.php on line 81
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 67
PHP Notice:  Use of undefined constant neighbors - assumed 'neighbors' in /home/05CCSO/prog.php on line 68
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 70
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 79
PHP Notice:  Use of undefined constant visited - assumed 'visited' in /home/05CCSO/prog.php on line 66