<?php
SUBWAY => 'едешь на метро' ,
FOOT => 'идешь пешком' ,
BUS => 'едешь на автобусе'
) ;
'pet' => 'ст. м. Петроградская' ,
'chk' => 'ст. м. Чкаловская' ,
'gor' => 'ст. м. Горьковская' ,
'spo' => 'ст. м. Спортивная' ,
'vas' => 'ст. м. Василеостровская' ,
'kre' => 'Петропавловская крепость' ,
'let' => 'Летний сад' ,
'dvo' => 'Дворцовая площадь' ,
'isa' => 'Исакиевский собор' ,
'nov' => 'Новая Голландия' ,
'ras' => 'Дом Раскольникова' ,
'gos' => 'Гостиный Двор' ,
'sen' => 'Сенная Площадь' ,
'vla' => 'ст. м. Владимирская' ,
'vit' => 'Витебский вокзал' ,
'teh' => 'Технологический Институт'
) ;
'chk' => canGet( 10 , BUS) ,
'gor' => canGet( 3 , SUBWAY)
) ,
'pet' => canGet( 10 , BUS) ,
'spo' => canGet( 3 , SUBWAY)
) ,
'pet' => canGet( 3 , BUS) ,
'kre' => canGet( 5 , FOOT) ,
'gos' => canGet( 6 , SUBWAY)
) ,
'chk' => canGet( 3 , SUBWAY) ,
'vas' => canGet( 10 , BUS) ,
'sen' => canGet( 7 , SUBWAY)
) ,
'spo' => canGet( 10 , BUS) ,
'gos' => canGet( 7 , SUBWAY) ,
'nov' => canGet( 11 , FOOT)
) ,
'gor' => canGet( 5 , FOOT)
) ,
'dvo' => canGet( 6 , FOOT) ,
'gos' => canGet( 7 , FOOT)
) ,
'isa' => canGet( 6 , FOOT) ,
'gos' => canGet( 6 , FOOT) ,
'let' => canGet( 6 , FOOT)
) ,
'dvo' => canGet( 6 , FOOT) ,
'nov' => canGet( 5 , FOOT)
) ,
'vas' => canGet( 11 , FOOT) ,
'isa' => canGet( 5 , FOOT) ,
'ras' => canGet( 7 , BUS)
) ,
'nov' => canGet( 7 , BUS) ,
'sen' => canGet( 3 , FOOT)
) ,
'vas' => canGet( 7 , SUBWAY) ,
'sen' => canGet( 3 , SUBWAY) ,
'dvo' => canGet( 6 , FOOT) ,
'gor' => canGet( 6 , SUBWAY) ,
'let' => canGet( 7 , FOOT) ,
'vla' => canGet( 7 , FOOT)
) ,
'ras' => canGet( 3 , FOOT) ,
'spo' => canGet( 7 , SUBWAY) ,
'gos' => canGet( 3 , SUBWAY) ,
'vla' => canGet( 4 , SUBWAY) ,
'vit' => canGet( 2 , SUBWAY) ,
'teh' => canGet( 3 , SUBWAY)
) ,
'sen' => canGet( 4 , SUBWAY) ,
'gos' => canGet( 7 , FOOT) ,
'vit' => canGet( 3 , SUBWAY)
) ,
'sen' => canGet( 2 , SUBWAY) ,
'teh' => canGet( 2 , SUBWAY) ,
'vla' => canGet( 3 , SUBWAY)
) ,
'sen' => canGet( 3 , SUBWAY) ,
'vit' => canGet( 2 , SUBWAY)
)
) ;
// Чтобы не писать много раз array('time' => ..., 'by' => ...), используем функцию.
$inf = 99999 ;
$start = 'pet' ;
$end = 'nov' ;
$path = [ $end ] ;
$visited = [ ] ;
$time = [ ] ;
foreach ( $paths as $graph => $value ) {
$time [ $graph ] = $inf ;
}
$time [ $start ] = 0 ;
function canGet( $time , $byWhat ) {
return array ( 'time' => $time , 'by' => $byWhat ) ; }
function findUnvisitedNodeWithLowestDistance( $time , $visited ) {
foreach ( $time as $t => $value ) {
}
}
}
function Dijkstra( $paths , $pos , $time , $visited , $start , $end , $path ) {
$visited [ ] = $pos ;
return findShortestPath( $paths , $time , $end , $path , $start ) ;
}
foreach ( $paths [ $pos ] as $g => $v ) {
if ( $time [ $g ] > $time [ $pos ] + $v [ 'time' ] ) {
$time [ $g ] = $time [ $pos ] + $v [ 'time' ] ;
}
}
$pos = findUnvisitedNodeWithLowestDistance( $time , $visited ) ;
return Dijkstra( $paths , $pos , $time , $visited , $start , $end , $path ) ;
}
function findShortestPath( $paths , $time , $pos , $path , $start ) { //Проблема в этом алгоритме
if ( $pos == $start ) {
return $A = [ 'path' => $path , 'time' => $time ] ;
}
foreach ( $paths [ $pos ] as $Graph => $value ) {
if ( $time [ $pos ] == $time [ $Graph ] + $value [ 'time' ] ) {
$path [ ] = $Graph ;
$pos = $Graph ;
return findShortestPath( $paths , $time , $pos , $path , $start ) ;
}
}
}
$A = Dijkstra( $paths , $start , $time , $visited , $start , $end , $path ) ;
$path = $A [ 'path' ] ;
$time = $A [ 'time' ] ;
echo "Начальная точка: " . $pointNames [ $path [ 0 ] ] . ". \n " ;
for ( $i = 0 ; $i != ( count ( $path ) - 1 ) ; $i ++ ) { echo "Из нее " . $transportName [ $paths [ $path [ $i ] ] [ $path [ $i + 1 ] ] [ 'by' ] ]
. " до точки " . $pointNames [ $path [ $i + 1 ] ]
. " за " . $paths [ $path [ $i ] ] [ $path [ $i + 1 ] ] [ 'time' ] . " мин.\n " ;
}
echo "В итоге ты попадаешь в точку " . $pointNames [ $end ] . " за " . $time [ $end ] . " минут." ;
PD9waHAKCmRlZmluZSgnU1VCV0FZJywgJ3N1YicpOwpkZWZpbmUoJ0ZPT1QnLCAnZm9vdCcpOwpkZWZpbmUoJ0JVUycsICdidXMnKTsKCiR0cmFuc3BvcnROYW1lID0gYXJyYXkoCiAgICBTVUJXQVkgPT4gJ9C10LTQtdGI0Ywg0L3QsCDQvNC10YLRgNC+JywKICAgIEZPT1QgPT4gJ9C40LTQtdGI0Ywg0L/QtdGI0LrQvtC8JywKICAgIEJVUyA9PiAn0LXQtNC10YjRjCDQvdCwINCw0LLRgtC+0LHRg9GB0LUnCik7CgokcG9pbnROYW1lcyA9IGFycmF5KAogICAgJ3BldCcgPT4gJ9GB0YIuINC8LiDQn9C10YLRgNC+0LPRgNCw0LTRgdC60LDRjycsCiAgICAnY2hrJyA9PiAn0YHRgi4g0LwuINCn0LrQsNC70L7QstGB0LrQsNGPJywKICAgICdnb3InID0+ICfRgdGCLiDQvC4g0JPQvtGA0YzQutC+0LLRgdC60LDRjycsCiAgICAnc3BvJyA9PiAn0YHRgi4g0LwuINCh0L/QvtGA0YLQuNCy0L3QsNGPJywKICAgICd2YXMnID0+ICfRgdGCLiDQvC4g0JLQsNGB0LjQu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V0KDMsIEJVUyksCiAgICAgICAgJ2tyZScgPT4gY2FuR2V0KDUsIEZPT1QpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg2LCBTVUJXQVkpCiAgICApLAogICAgJ3NwbycgPT4gYXJyYXkoCiAgICAgICAgJ2NoaycgPT4gY2FuR2V0KDMsIFNVQldBWSksCiAgICAgICAgJ3ZhcycgPT4gY2FuR2V0KDEwLCBCVVMpLAogICAgICAgICdzZW4nID0+IGNhbkdldCg3LCBTVUJXQVkpCiAgICApLAogICAgJ3ZhcycgPT4gYXJyYXkoCiAgICAgICAgJ3NwbycgPT4gY2FuR2V0KDEwLCBCVVMpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg3LCBTVUJXQVkpLAogICAgICAgICdub3YnID0+IGNhbkdldCgxMSwgRk9PVCkKICAgICksCiAgICAna3JlJyA9PiBhcnJheSgKICAgICAgICAnZ29yJyA9PiBjYW5HZXQoNSwgRk9PVCkKICAgICksCiAgICAnbGV0JyA9PiBhcnJheSgKICAgICAgICAnZHZvJyA9PiBjYW5HZXQoNiwgRk9PVCksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDcsIEZPT1QpCiAgICApLAogICAgJ2R2bycgPT4gYXJyYXkoCiAgICAgICAgJ2lzYScgPT4gY2FuR2V0KDYsIEZPT1QpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg2LCBGT09UKSwKICAgICAgICAnbGV0JyA9PiBjYW5HZXQoNiwgRk9PVCkKICAgICksCiAgICAnaXNhJyA9PiBhcnJheSgKICAgICAgICAnZHZvJyA9PiBjYW5HZXQoNiwgRk9PVCksCiAgICAgICAgJ25vdicgPT4gY2FuR2V0KDUsIEZPT1QpCiAgICApLAogICAgJ25vdicgPT4gYXJyYXkoCiAgICAgICAgJ3ZhcycgPT4gY2FuR2V0KDExLCBGT09UKSwKICAgICAgICAnaXNhJyA9PiBjYW5HZXQoNSwgRk9PVCksCiAgICAgICAgJ3JhcycgPT4gY2FuR2V0KDcsIEJVUykKICAgICksCiAgICAncmFzJyA9PiBhcnJheSgKICAgICAgICAnbm92JyA9PiBjYW5HZXQoNywgQlVTKSwKICAgICAgICAnc2VuJyA9PiBjYW5HZXQoMywgRk9PVCkKICAgICksCiAgICAnZ29zJyA9PiBhcnJheSgKICAgICAgICAndmFzJyA9PiBjYW5HZXQoNywgU1VCV0FZKSwKICAgICAgICAnc2VuJyA9PiBjYW5HZXQoMywgU1VCV0FZKSwKICAgICAgICAnZHZvJyA9PiBjYW5HZXQoNiwgRk9PVCksCiAgICAgICAgJ2dvcicgPT4gY2FuR2V0KDYsIFNVQldBWSksCiAgICAgICAgJ2xldCcgPT4gY2FuR2V0KDcsIEZPT1QpLAogICAgICAgICd2bGEnID0+IGNhbkdldCg3LCBGT09UKQogICAgKSwKICAgICdzZW4nID0+IGFycmF5KAogICAgICAgICdyYXMnID0+IGNhbkdldCgzLCBGT09UKSwKICAgICAgICAnc3BvJyA9PiBjYW5HZXQoNywgU1VCV0FZKSwKICAgICAgICAnZ29zJyA9PiBjYW5HZXQoMywgU1VCV0FZKSwKICAgICAgICAndmxhJyA9PiBjYW5HZXQoNCwgU1VCV0FZKSwKICAgICAgICAndml0JyA9PiBjYW5HZXQoMiwgU1VCV0FZKSwKICAgICAgICAndGVoJyA9PiBjYW5HZXQoMywgU1VCV0FZKQogICAgKSwKICAgICd2bGEnID0+IGFycmF5KAogICAgICAgICdzZW4nID0+IGNhbkdldCg0LCBTVUJXQVkpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg3LCBGT09UKSwKICAgICAgICAndml0JyA9PiBjYW5HZXQoMywgU1VCV0FZKQogICAgKSwKICAgICd2aXQnID0+IGFycmF5KAogICAgICAgICdzZW4nID0+IGNhbkdldCgyLCBTVUJXQVkpLAogICAgICAgICd0ZWgnID0+IGNhbkdldCgyLCBTVUJXQVkpLAogICAgICAgICd2bGEnID0+IGNhbkdldCgzLCBTVUJXQVkpCiAgICApLAogICAgJ3RlaCcgPT4gYXJyYXkoCiAgICAg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