結果

問題 No.1 道のショートカット
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-06 23:00:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 1,562 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 8,864 KB
最終ジャッジ日時 2023-09-27 21:46:54
合計ジャッジ時間 2,532 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,272 KB
testcase_01 AC 14 ms
7,940 KB
testcase_02 AC 14 ms
7,892 KB
testcase_03 AC 14 ms
7,848 KB
testcase_04 AC 15 ms
8,308 KB
testcase_05 AC 15 ms
8,280 KB
testcase_06 AC 15 ms
8,336 KB
testcase_07 AC 15 ms
8,184 KB
testcase_08 AC 32 ms
8,456 KB
testcase_09 AC 23 ms
8,196 KB
testcase_10 AC 21 ms
8,308 KB
testcase_11 AC 23 ms
8,228 KB
testcase_12 AC 39 ms
8,748 KB
testcase_13 AC 42 ms
8,864 KB
testcase_14 AC 15 ms
8,240 KB
testcase_15 AC 15 ms
8,360 KB
testcase_16 AC 18 ms
8,348 KB
testcase_17 AC 16 ms
8,304 KB
testcase_18 AC 17 ms
8,224 KB
testcase_19 AC 18 ms
8,288 KB
testcase_20 AC 18 ms
8,228 KB
testcase_21 AC 19 ms
8,268 KB
testcase_22 AC 18 ms
8,348 KB
testcase_23 AC 32 ms
8,196 KB
testcase_24 AC 34 ms
8,228 KB
testcase_25 AC 22 ms
8,200 KB
testcase_26 AC 20 ms
8,296 KB
testcase_27 AC 29 ms
8,164 KB
testcase_28 AC 14 ms
8,276 KB
testcase_29 AC 21 ms
8,224 KB
testcase_30 AC 14 ms
8,156 KB
testcase_31 AC 17 ms
8,156 KB
testcase_32 AC 26 ms
8,156 KB
testcase_33 AC 22 ms
8,208 KB
testcase_34 AC 27 ms
8,464 KB
testcase_35 AC 16 ms
8,348 KB
testcase_36 AC 18 ms
8,328 KB
testcase_37 AC 15 ms
8,360 KB
testcase_38 AC 15 ms
8,356 KB
testcase_39 AC 17 ms
8,308 KB
testcase_40 AC 15 ms
8,312 KB
testcase_41 AC 16 ms
8,200 KB
testcase_42 AC 15 ms
8,368 KB
testcase_43 AC 14 ms
8,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N = int(input())  # 町の数
    C = int(input())  # 手持ちのお金
    V = int(input())  # 道の数
    S = list(map(int, input().split()))  # 道の始点
    T = list(map(int, input().split()))  # 道の終点
    Y = list(map(int, input().split()))  # 道のコスト(料金)
    M = list(map(int, input().split()))  # 道のコスト(時間)
    roads = [[] for i in range(N)]
    for s, t, y, m in zip(S, T, Y, M):
        roads[s-1].append((t-1, y, m))
    return N, C, V, roads


def solve(N, C, V, roads):
    '''
    dp[n][c]: 町 n に所持金額 c 円でたどりつくときの、最短所要時間
    dp[n][c] の状態から、cost, time でmに行けるとすると、
    dp[m][c-cost] = min(dp[m][c-cost], dp[n][c] + time)
    で更新していけばよい。
    '''
    if N == 1:
        return 0
    if C == 0:
        return -1
    dp = [[float('inf')] * (C + 1) for c in range(N)]
    dp[0][C] = 0
    for pos in range(N):
        dp_pos = dp[pos]
        for next_pos, cost, time in roads[pos]:
            dp_next = dp[next_pos]
            for c in range(C, 0, -1):
                new_c = c - cost
                if new_c < 0:
                    break
                new_time = dp_pos[c] + time
                if new_time < dp_next[new_c]:
                    dp_next[new_c] = new_time
    min_time = min(dp[N-1])
    if min_time == float('inf'):
        return -1
    else:
        return min_time

if __name__ == '__main__':
    N, C, V, roads = read_data()
    print(solve(N, C, V, roads))
0