<?php
header ( "Content-Type: text/plain; charset=utf-8" ) ; ?>
<?php
/* http://d...content-available-to-author-only...2.net/ */
SUBWAY => 'едешь на метро' ,
FOOT => 'идешь пешком' ,
BUS => 'едешь на автобусе'
) ;
$startPoint = 'pet' ; // Петроградская
$endPoint = 'teh' ; // Новая Голландия
'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
) ;
}
$time = 0 ;
function makeOneStep( $paths , $startPoint , $endPoint , & $time , $pathDone )
{
$pathDone [ ] = $startPoint ;
if ( isset ( $paths [ $startPoint ] [ $endPoint ] ) ) { $pathDone [ ] = $endPoint ;
$time += $paths [ $startPoint ] [ $endPoint ] [ 'time' ] ;
$result [ ] = $pathDone ;
$result [ 'time' ] = $time ;
return $result ;
}
foreach ( $paths [ $startPoint ] as $k => $station ) {
$time += $paths [ $startPoint ] [ $k ] [ 'time' ] ;
$newPath = makeOneStep( $paths , $k , $endPoint , $time , $pathDone ) ;
if ( $newPath ) {
if ( ! isset ( $lowerTime ) || ( $time < $lowerTime ) ) $lowerTime = $time ;
$shortest = $newPath ;
}
}
}
$result = $shortest ;
$result [ 'time' ] = $lowerTime ;
return $result ;
}
print_r ( makeOneStep
( $paths , $startPoint , $endPoint , $time , $pathDone ) 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PiBjYW5HZXQoNiwgU1VCV0FZKQogICAgKSwKICAgIAogICAgJ3NwbycgPT4gYXJyYXkoCiAgICAgICAgJ2NoaycgPT4gY2FuR2V0KDMsIFNVQldBWSksCiAgICAgICAgJ3ZhcycgPT4gY2FuR2V0KDEwLCBCVVMpLAogICAgICAgICdzZW4nID0+IGNhbkdldCg3LCBTVUJXQVkpCiAgICApLAogICAgCiAgICAndmFzJyA9PiBhcnJheSgKICAgICAgICAnc3BvJyA9PiBjYW5HZXQoMTAsIEJVUyksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDcsIFNVQldBWSksCiAgICAgICAgJ25vdicgPT4gY2FuR2V0KDExLCBGT09UKQogICAgKSwKICAgIAogICAgJ2tyZScgPT4gYXJyYXkoCiAgICAgICAgJ2dvcicgPT4gY2FuR2V0KDUsIEZPT1QpCiAgICApLAogICAgCiAgICAnbGV0JyA9PiBhcnJheSgKICAgICAgICAnZHZvJyA9PiBjYW5HZXQoNiwgRk9PVCksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDcsIEZPT1QpCiAgICApLAogICAgCiAgICAnZHZvJyA9PiBhcnJheSgKICAgICAgICAnaXNhJyA9PiBjYW5HZXQoNiwgRk9PVCksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDYsIEZPT1QpLAogICAgICAgICdsZXQnID0+IGNhbkdldCg2LCBGT09UKQogICAgKSwKICAgIAogICAgJ2lzYScgPT4gYXJyYXkoCiAgICAgICAgJ2R2bycgPT4gY2FuR2V0KDYsIEZPT1QpLAogICAgICAgICdub3YnID0+IGNhbkdldCg1LCBGT09UKQogICAgKSwKICAgIAogICAgJ25vdicgPT4gYXJyYXkoCiAgICAgICAgJ3ZhcycgPT4gY2FuR2V0KDExLCBGT09UKSwKICAgICAgICAnaXNhJyA9PiBjYW5HZXQoNSwgRk9PVCksCiAgICAgICAgJ3JhcycgPT4gY2FuR2V0KDcsIEJVUykKICAgICksCiAgICAKICAgICdyYXMnID0+IGFycmF5KAogICAgICAgICdub3YnID0+IGNhbkdldCg3LCBCVVMpLAogICAgICAgICdzZW4nID0+IGNhbkdldCgzLCBGT09UKQogICAgKSwKICAgIAogICAgJ2dvcycgPT4gYXJyYXkoCiAgICAgICAgJ3ZhcycgPT4gY2FuR2V0KDcsIFNVQldBWSksCiAgICAgICAgJ3NlbicgPT4gY2FuR2V0KDMsIFNVQldBWSksCiAgICAgICAgJ2R2bycgPT4gY2FuR2V0KDYsIEZPT1QpLAogICAgICAgICdnb3InID0+IGNhbkdldCg2LCBTVUJXQVkpLAogICAgICAgICdsZXQnID0+IGNhbkdldCg3LCBGT09UKSwKICAgICAgICAndmxhJyA9PiBjYW5HZXQoNywgRk9PVCkKICAgICksCiAgICAKICAgICdzZW4nID0+IGFycmF5KAogICAgICAgICdyYXMnID0+IGNhbkdldCgzLCBGT09UKSwKICAgICAgICAnc3BvJyA9PiBjYW5HZXQoNywgU1VCV0FZKSwKICAgICAgICAnZ29zJyA9PiBjYW5HZXQoMywgU1VCV0FZKSwKICAgICAgICAndmxhJyA9PiBjYW5HZXQoNCwgU1VCV0FZKSwKICAgICAgICAndml0JyA9PiBjYW5HZXQoMiwgU1VCV0FZKSwKICAgICAgICAndGVoJyA9PiBjYW5HZXQoMywgU1VCV0FZKQogICAgKSwKICAgIAogICAgJ3ZsYScgPT4gYXJyYXkoCiAgICAgICAgJ3NlbicgPT4gY2FuR2V0KDQsIFNVQldBWSksCiAgICAgICAgJ2dvcycgPT4gY2FuR2V0KDcsIEZPT1QpLAogICAgICAgICd2aXQnID0+IGNhbkdldCgzLCBTVUJXQVkpCiAgICApLAogICAgCiAgICAndml0JyA9PiBhcnJheSgKICAgICAgICAnc2VuJyA9PiBjYW5HZXQoMiwgU1VCV0FZKSwKICAgICAgICAndGVoJyA9PiBjYW5HZXQoMiwgU1VCV0FZKSwKICAgICAgICAndmxhJyA9PiBjYW5HZXQoMywgU1VCV0FZKQogICAgKSwKICAgIAogICAgJ3RlaCcgPT4gYXJyYXkoC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hcnJheSgpOwogICAgCiAgICBmb3JlYWNoICgkcGF0aHNbJHN0YXJ0UG9pbnRdIGFzICRrID0+ICRzdGF0aW9uKSB7CiAgICAgICAgaWYgKCFpbl9hcnJheSgkaywgJHBhdGhEb25lKSkgewogICAgICAgICAgICAkdGltZSArPSAkcGF0aHNbJHN0YXJ0UG9pbnRdWyRrXVsndGltZSddOwogICAgICAgICAgICAkbmV3UGF0aCA9IG1ha2VPbmVTdGVwKCRwYXRocywgJGssICRlbmRQb2ludCwgJHRpbWUsICRwYXRoRG9uZSk7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAoJG5ld1BhdGgpIHsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgaWYgKCFpc3NldCgkbG93ZXJUaW1lKSB8fCAoJHRpbWUgPCAkbG93ZXJUaW1lKSkKICAgICAgICAgICAgICAgICAgICAkbG93ZXJUaW1lID0gJHRpbWU7CiAgICAgICAgICAgICAgICAkc2hvcnRlc3QgPSAkbmV3UGF0aDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgICRyZXN1bHQgICAgICAgICA9ICRzaG9ydGVzdDsKICAgICRyZXN1bHRbJ3RpbWUnXSA9ICRsb3dlclRpbWU7CiAgICByZXR1cm4gJHJlc3VsdDsKICAgIAogICAgCn0KcHJpbnRfcihtYWtlT25lU3RlcCgkcGF0aHMsICRzdGFydFBvaW50LCAkZW5kUG9pbnQsICR0aW1lLCAkcGF0aERvbmUpKTsK