結果
| 問題 | No.3393 Move on Highway |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-12-06 01:28:02 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 2,592 ms / 3,000 ms |
| コード長 | 2,118 bytes |
| 記録 | |
| コンパイル時間 | 330 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 161,212 KB |
| 最終ジャッジ日時 | 2025-12-06 01:29:00 |
| 合計ジャッジ時間 | 53,916 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
## https://yukicoder.me/problems/no/3393
import heapq
MAX_VALUE = 10 ** 18
def main():
N, M, C = map(int, input().split())
next_nodes = [[] for _ in range(N)]
for _ in range(M):
u, v, w = map(int, input().split())
next_nodes[u - 1].append((v - 1, w))
next_nodes[v - 1].append((u - 1, w))
# 逆方向からの探索
fix = [[MAX_VALUE] * N for _ in range(2)]
seen = [[MAX_VALUE] * N for _ in range(2)]
queue = []
seen[0][N - 1] = 0
heapq.heappush(queue, (0, 0, N - 1))
while len(queue) > 0:
cost, state, u = heapq.heappop(queue)
if fix[state][u] < MAX_VALUE:
continue
fix[state][u] = cost
for v, w in next_nodes[u]:
if fix[state][v] == MAX_VALUE:
new_cost = cost + w + C
if seen[state][v] > new_cost:
seen[state][v] = new_cost
heapq.heappush(queue, (new_cost, state, v))
if state == 0:
new_state = 1
if fix[new_state][v] == MAX_VALUE:
new_cost = cost + C
if seen[new_state][v] > new_cost:
seen[new_state][v] = new_cost
heapq.heappush(queue, (new_cost, new_state, v))
back_fix = fix
# 順方向からの探索
fix = [MAX_VALUE] * N
seen = [MAX_VALUE] * N
queue = []
seen[0] = 0
heapq.heappush(queue, (0, 0))
while len(queue) > 0:
cost, u = heapq.heappop(queue)
if fix[u] < MAX_VALUE:
continue
fix[u] = cost
for v, w in next_nodes[u]:
if fix[v] == MAX_VALUE:
new_cost = cost + w + C
if seen[v] > new_cost:
seen[v] = new_cost
heapq.heappush(queue, (new_cost, v))
front_fix = fix
for i in range(1, N):
ans = front_fix[i] + min(back_fix[0][i], back_fix[1][i])
ans = min(ans, front_fix[N - 1])
print(ans)
if __name__ == "__main__":
main()