<?php
/* http://d...content-available-to-author-only...2.net/ */
SUBWAY => 'едешь на метро' ,
FOOT => 'идешь пешком' ,
BUS => 'едешь на автобусе'
) ;
$startPoint = 'pet' ; // Петроградская
$endPoint = 'nov' ; // Новая Голландия
'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' => ...), используем функцию.
«canGet» переводится как «можно попасть» */
function canGet( $time , $byWhat )
{
'time' => $time ,
'by' => $byWhat
) ;
}
function oneStep( $paths , $point , $target )
{
$inf = 100000 ;
$visited [ ] = $point ;
foreach ( $paths as $key => $value ) {
$time [ $key ] = $inf ;
}
$time [ $point ] = 0 ;
foreach ( $paths [ $point ] as $key => $val ) {
$time [ $key ] = $paths [ $point ] [ $key ] [ 'time' ] ;
$key
) ;
}
foreach ( $paths as $key => $val ) {
if ( $time [ $key ] == $inf || ( in_array ( $key , $visited ) ) ) { continue ;
}
foreach ( $paths [ $key ] as $subkey => $val ) {
if ( $time [ $subkey ] > $time [ $key ] + $paths [ $key ] [ $subkey ] [ 'time' ] ) {
$time [ $subkey ] = $time [ $key ] + $paths [ $key ] [ $subkey ] [ 'time' ] ;
$pathDone [ $subkey ] = $pathDone [ $key ] ;
$pathDone [ $subkey ] [ ] = $subkey ;
}
}
$visited [ ] = $paths [ $key ] ;
}
}
$result [ 'time' ] = $time [ $target ] ;
$result [ 'path' ] = $pathDone [ $target ] ;
return $result ;
}
function showPath( $paths , $point , $target , $pointNames , $transportName )
{
$result = oneStep( $paths , $point , $target ) ;
$previous = $point ;
echo "Начальная точка: $pointNames [$point ]\n " ;
foreach ( $result [ 'path' ] as $key => $value ) {
$translate = $paths [ $previous ] [ $value ] [ 'by' ] ;
echo "Из неё $transportName [$translate ] в точку $pointNames [$value ] {$paths [$previous ][$value ]['time']} мин\n " ;
$previous = $value ;
}
echo "В итоге ты попадешь в точку $pointNames [$target ] за " ;
echo $result [ 'time' ] ;
echo "минут" ;
}
showPath( $paths , $startPoint , $endPoint , $pointNames , $transportName ) ;
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7Ci8qIGh0dHA6Ly9kLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi4yLm5ldC8gKi8KCmRlZmluZSgnU1VCV0FZJywgJ3N1YicpOwpkZWZpbmUoJ0ZPT1QnLCAnZm9vdCcpOwpkZWZpbmUoJ0JVUycsICdidXMnKTsKCiR0cmFuc3BvcnROYW1lID0gYXJyYXkoCiAgICBTVUJXQVkgPT4gJ9C10LTQtdGI0Ywg0L3QsCDQvNC10YLRgNC+JywKICAgIEZPT1QgPT4gJ9C40LTQtdGI0Ywg0L/QtdGI0LrQvtC8JywKICAgIEJVUyA9PiAn0LXQtNC10YjRjCDQvdCwINCw0LLRgtC+0LHRg9GB0LUnCik7Cgokc3RhcnRQb2ludCA9ICdwZXQnOyAvLyDQn9C10YLRgNC+0LPRgNCw0LTRgdC60LDRjwokZW5kUG9pbnQgICA9ICdub3YnOyAvLyDQndC+0LLQsNGPINCT0L7Qu9C70LDQvdC00LjRjwoKJHBvaW50TmFtZXMgPSBhcnJheSgKICAgICdwZXQnID0+ICfRgdGCLiDQvC4g0J/QtdGC0YDQvtCz0YDQsNC00YHQutCw0Y8nLAogICAgJ2NoaycgPT4gJ9GB0YIuINC8LiDQp9C60LDQu9C+0LLRgdC60LDRjycsCiAgICAnZ29yJyA9PiAn0YHRgi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BldCcgPT4gY2FuR2V0KDEwLCBCVVMpLAogICAgICAgICdzcG8nID0+IGNhbkdldCgzLCBTVUJXQVkpCiAgICApLAogICAgCiAgICAnZ29yJyA9PiBhcnJheSgKICAgICAgICAncGV0JyA9PiBjYW5HZXQoMywgQlVTKSwKICAgICAgICAna3JlJyA9PiBjYW5HZXQoNSwgRk9PVCksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDYsIFNVQldBWSkKICAgICksCiAgICAKICAgICdzcG8nID0+IGFycmF5KAogICAgICAgICdjaGsnID0+IGNhbkdldCgzLCBTVUJXQVkpLAogICAgICAgICd2YXMnID0+IGNhbkdldCgxMCwgQlVTKSwKICAgICAgICAnc2VuJyA9PiBjYW5HZXQoNywgU1VCV0FZKQogICAgKSwKICAgIAogICAgJ3ZhcycgPT4gYXJyYXkoCiAgICAgICAgJ3NwbycgPT4gY2FuR2V0KDEwLCBCVVMpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg3LCBTVUJXQVkpLAogICAgICAgICdub3YnID0+IGNhbkdldCgxMSwgRk9PVCkKICAgICksCiAgICAKICAgICdrcmUnID0+IGFycmF5KAogICAgICAgICdnb3InID0+IGNhbkdldCg1LCBGT09UKQogICAgKSwKICAgIAogICAgJ2xldCcgPT4gYXJyYXkoCiAgICAgICAgJ2R2bycgPT4gY2FuR2V0KDYsIEZPT1QpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg3LCBGT09UKQogICAgKSwKICAgIAogICAgJ2R2bycgPT4gYXJyYXkoCiAgICAgICAgJ2lzYScgPT4gY2FuR2V0KDYsIEZPT1QpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg2LCBGT09UKSwKICAgICAgICAnbGV0JyA9PiBjYW5HZXQoNiwgRk9PVCkKICAgICksCiAgICAKICAgICdpc2EnID0+IGFycmF5KAogICAgICAgICdkdm8nID0+IGNhbkdldCg2LCBGT09UKSwKICAgICAgICAnbm92JyA9PiBjYW5HZXQoNSwgRk9PVCkKICAgICksCiAgICAKICAgICdub3YnID0+IGFycmF5KAogICAgICAgICd2YXMnID0+IGNhbkdldCgxMSwgRk9PVCksCiAgICAgICAgJ2lzYScgPT4gY2FuR2V0KDUsIEZPT1QpLAogICAgICAgICdyYXMnID0+IGNhbkdldCg3LCBCVVMpCiAgICApLAogICAgCiAgICAncmFzJyA9PiBhcnJheSgKICAgICAgICAnbm92JyA9PiBjYW5HZXQoNywgQlVTKSwKICAgICAgICAnc2VuJyA9PiBjYW5HZXQoMywgRk9PVCkKICAgICksCiAgICAKICAgICdnb3MnID0+IGFycmF5KAogICAgICAgICd2YXMnID0+IGNhbkdldCg3LCBTVUJXQVkpLAogICAgICAgICdzZW4nID0+IGNhbkdldCgzLCBTVUJXQVkpLAogICAgICAgICdkdm8nID0+IGNhbkdldCg2LCBGT09UKSwKICAgICAgICAnZ29yJyA9PiBjYW5HZXQoNiwgU1VCV0FZKSwKICAgICAgICAnbGV0JyA9PiBjYW5HZXQoNywgRk9PVCksCiAgICAgICAgJ3ZsYScgPT4gY2FuR2V0KDcsIEZPT1QpCiAgICApLAogICAgCiAgICAnc2VuJyA9PiBhcnJheSgKICAgICAgICAncmFzJyA9PiBjYW5HZXQoMywgRk9PVCksCiAgICAgICAgJ3NwbycgPT4gY2FuR2V0KDcsIFNVQldBWSksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDMsIFNVQldBWSksCiAgICAgICAgJ3ZsYScgPT4gY2FuR2V0KDQsIFNVQldBWSksCiAgICAgICAgJ3ZpdCcgPT4gY2FuR2V0KDIsIFNVQldBWSksCiAgICAgICAgJ3RlaCcgPT4gY2FuR2V0KDMsIFNVQldBWSkKICAgICksCiAgICAKICAgICd2bGEnID0+IGFycmF5KAogICAgICAgICdzZW4nID0+IGNhbkdldCg0LCBTVUJXQVkpLAogICAgICAgICdnb3MnID0+IGNhbkdldCg3LCBGT09UKSwKICAgICAgICAndml0JyA9PiBjYW5HZXQoMywgU1VCV0FZKQogICAgKSwKICAgIAogICAgJ3ZpdCcgPT4gYXJyYXkoCiAgICAgICAgJ3NlbicgPT4gY2FuR2V0KDIsIFNVQldBWSksCiAgICAgICAgJ3RlaCcgPT4gY2FuR2V0KDIsIFNVQldBWSksCiAgICAgICAgJ3ZsYScgPT4gY2FuR2V0KDMsIFNVQldBWSkKICAgICksCiAgICAKICAgICd0ZWgnID0+IGFycmF5KAogICAgICAgICdzZW4nID0+IGNhbkdldCgzLCBTVUJXQVkpLAogICAgICAgICd2aXQnID0+IGNhbkdldCgyLCBTVUJXQVkpCiAgICApCik7CgovKiDQp9GC0L7QsdGLINC90LUg0L/QuNGB0LDRgtGMINC80L3QvtCz0L4g0YDQsNC3IGFycmF5KCd0aW1lJyA9PiAuLi4sICdieScgPT4gLi4uKSwg0LjRgdC/0L7Qu9GM0LfRg9C10Lwg0YTRg9C90LrRhtC40Y4uIArCq2NhbkdldMK7INC/0LXRgNC10LLQvtC00LjRgtGB0Y8g0LrQsNC6IMKr0LzQvtC20L3QviDQv9C+0L/QsNGB0YLRjMK7ICovCmZ1bmN0aW9uIGNhbkdldCgkdGltZSwgJGJ5V2hhdCkKewogICAgcmV0dXJuIGFycmF5KAogICAgICAgICd0aW1lJyA9PiAkdGltZSwKICAgICAgICAnYnknID0+ICRieVdoYXQKICAgICk7Cn0KCgoKZnVuY3Rpb24gb25lU3RlcCgkcGF0aHMsICRwb2ludCwgJHRhcmdldCkKewogICAgJGluZiAgICAgICA9IDEwMDAwMDsKICAgICR0aW1lICAgICAgPSBhcnJheSgpOwogICAgJHZpc2l0ZWQgICA9IGFycmF5KCk7CiAgICAkdmlzaXRlZFtdID0gJHBvaW50OwogICAgJHBhdGhEb25lICA9IGFycmF5KCk7CiAgICBmb3JlYWNoICgkcGF0aHMgYXMgJGtleSA9PiAkdmFsdWUpIHsKICAgICAgICBhc29ydCgkcGF0aHNbJGtleV0pOwogICAgICAgICR0aW1lWyRrZXldID0gJGluZjsKICAgIH0KICAgICR0aW1lWyRwb2ludF0gPSAwOwogICAgZm9yZWFjaCAoJHBhdGhzWyRwb2ludF0gYXMgJGtleSA9PiAkdmFsKSB7CiAgICAgICAgJHRpbWVbJGtleV0gICAgID0gJHBhdGhzWyRwb2ludF1bJGtleV1bJ3RpbWUnXTsKICAgICAgICAkcGF0aERvbmVbJGtleV0gPSBhcnJheSgKICAgICAgICAgICAgJGtleQogICAgICAgICk7CiAgICAgICAgCiAgICB9CiAgICAKICAgIHdoaWxlKGNvdW50KCR2aXNpdGVkKTxjb3VudCgkcGF0aHMpKXsKICAgICAgICBmb3JlYWNoICgkcGF0aHMgYXMgJGtleSA9PiAkdmFsKSB7CiAgICAgICAgICAgIGlmICgkdGltZVska2V5XSA9PSAkaW5mIHx8IChpbl9hcnJheSgka2V5LCAkdmlzaXRlZCkpKSB7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3JlYWNoICgkcGF0aHNbJGtleV0gYXMgJHN1YmtleSA9PiAkdmFsKSB7CiAgICAgICAgICAgICAgICBpZiAoJHRpbWVbJHN1YmtleV0gPiAkdGltZVska2V5XSArICRwYXRoc1ska2V5XVskc3Via2V5XVsndGltZSddKSB7CiAgICAgICAgICAgICAgICAgICAgJHRpbWVbJHN1YmtleV0gICAgICAgPSAkdGltZVska2V5XSArICRwYXRoc1ska2V5XVskc3Via2V5XVsndGltZSddOwogICAgICAgICAgICAgICAgICAgICRwYXRoRG9uZVskc3Via2V5XSAgID0gJHBhdGhEb25lWyRrZXldOwogICAgICAgICAgICAgICAgICAgICRwYXRoRG9uZVskc3Via2V5XVtdID0gJHN1YmtleTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICAkdmlzaXRlZFtdID0gJHBhdGhzWyRrZXldOwogICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgCiAgICB9CiAgICAkcmVzdWx0ICAgICAgICAgPSBhcnJheSgpOwogICAgJHJlc3VsdFsndGltZSddID0gJHRpbWVbJHRhcmdldF07CiAgICAkcm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