結果

問題 No.1 道のショートカット
ユーザー 綾地寧々綾地寧々
提出日時 2015-05-14 22:56:30
言語 PHP
(8.2.11)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 1,692 bytes
コンパイル時間 1,317 ms
コンパイル使用メモリ 18,416 KB
実行使用メモリ 18,924 KB
最終ジャッジ日時 2023-09-27 21:44:15
合計ジャッジ時間 3,756 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
18,880 KB
testcase_01 AC 15 ms
18,848 KB
testcase_02 AC 16 ms
18,852 KB
testcase_03 AC 16 ms
18,800 KB
testcase_04 AC 16 ms
18,832 KB
testcase_05 AC 16 ms
18,852 KB
testcase_06 AC 15 ms
18,628 KB
testcase_07 AC 16 ms
18,860 KB
testcase_08 AC 41 ms
18,856 KB
testcase_09 AC 21 ms
18,720 KB
testcase_10 AC 26 ms
18,860 KB
testcase_11 AC 17 ms
18,724 KB
testcase_12 AC 22 ms
18,852 KB
testcase_13 AC 23 ms
18,864 KB
testcase_14 AC 16 ms
18,876 KB
testcase_15 AC 16 ms
18,836 KB
testcase_16 AC 18 ms
18,832 KB
testcase_17 AC 16 ms
18,836 KB
testcase_18 AC 16 ms
18,604 KB
testcase_19 AC 17 ms
18,636 KB
testcase_20 AC 17 ms
18,832 KB
testcase_21 AC 16 ms
18,768 KB
testcase_22 AC 16 ms
18,916 KB
testcase_23 AC 31 ms
18,812 KB
testcase_24 AC 20 ms
18,828 KB
testcase_25 AC 16 ms
18,800 KB
testcase_26 AC 17 ms
18,840 KB
testcase_27 AC 24 ms
18,876 KB
testcase_28 AC 16 ms
18,836 KB
testcase_29 AC 24 ms
18,924 KB
testcase_30 AC 17 ms
18,768 KB
testcase_31 AC 17 ms
18,864 KB
testcase_32 AC 21 ms
18,692 KB
testcase_33 AC 18 ms
18,800 KB
testcase_34 AC 20 ms
18,796 KB
testcase_35 AC 16 ms
18,788 KB
testcase_36 AC 20 ms
18,844 KB
testcase_37 AC 21 ms
18,836 KB
testcase_38 AC 16 ms
18,804 KB
testcase_39 AC 16 ms
18,832 KB
testcase_40 AC 17 ms
18,748 KB
testcase_41 AC 16 ms
18,860 KB
testcase_42 AC 16 ms
18,796 KB
testcase_43 AC 16 ms
18,836 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$g_towns = trim(fgets(STDIN));
$g_money = trim(fgets(STDIN));
$g_roads = trim(fgets(STDIN));
$g_departure = explode(" ",trim(fgets(STDIN)));
$g_arrival = explode(" ",trim(fgets(STDIN)));
$g_cost = explode(" ",trim(fgets(STDIN)));
$g_time = explode(" ",trim(fgets(STDIN)));

$ret = move(1,$g_money,0,FALSE);
if ( $ret === FALSE ) {
    echo "-1\n";
}
else {
    echo $ret.PHP_EOL;
}

// 移動をおこなう関数
function move($start, $money, $time, $maxtime) {
    global $g_towns,$g_departure,$g_arrival,$g_cost,$g_time;
    // ----- 再帰呼出し終了条件の判定 ----------
    // コストが最大値を超えてしまった場合
    if ( $money < 0 ) {
        return FALSE;
    }
    // 最後の街に到着した場合
    else if ( $start == $g_towns ) {
        return $time;
    }
    // 他のルートより時間がかかったことを検出した場合
    else if ( ($maxtime !== FALSE) && ($maxtime <= $time) ) {
        return FALSE;
    }
    // ----- 再帰呼出しをおこなう ------------
    $min = $maxtime;
    foreach ( $g_departure as $index => $dep ) {
        // 行き先が見つかった
        if ( $dep == $start ) {
            $newtime = move($g_arrival[$index],$money-$g_cost[$index],$time+$g_time[$index],$min);
            if ( $newtime === FALSE ) {
                // 今回のルートでは最後まで辿りつけなかったので何もしない。
            }
            else if ( $min === FALSE ) {
                $min = $newtime;
            }
            else {
                $min = min($min, $newtime);
            }
        }
    }
    // ----- 探した最短時間での結果を返却する
    return $min;
}
0