結果
| 問題 |
No.1 道のショートカット
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-08-06 23:00:44 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 64 ms / 5,000 ms |
| コード長 | 1,562 bytes |
| コンパイル時間 | 249 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 11,136 KB |
| 最終ジャッジ日時 | 2024-07-20 16:16:23 |
| 合計ジャッジ時間 | 2,930 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
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))