結果
| 問題 |
No.2569 はじめてのおつかいHard
|
| コンテスト | |
| ユーザー |
prd_xxx
|
| 提出日時 | 2023-12-02 16:36:58 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 870 ms / 2,000 ms |
| コード長 | 1,403 bytes |
| コンパイル時間 | 194 ms |
| コンパイル使用メモリ | 82,556 KB |
| 実行使用メモリ | 144,096 KB |
| 最終ジャッジ日時 | 2025-06-20 01:55:59 |
| 合計ジャッジ時間 | 8,881 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 12 |
ソースコード
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
UVT = [tuple(map(int,input().split())) for _ in range(M)]
es = [[] for _ in range(N)]
rs = [[] for _ in range(N)]
for u,v,t in UVT:
u,v = u-1,v-1
es[v].append((u,t))
rs[u].append((v,t))
A,B = N-1,N-2
INF = 10**18
from heapq import heappop,heappush
hq = [(0,A)]
distA = [INF] * N
distA[A] = 0
while hq:
d,v = heappop(hq)
if d > distA[v]: continue
for to,c in es[v]:
if distA[to] <= d+c: continue
distA[to] = d+c
heappush(hq, (d+c, to))
hq = [(0,B)]
distB = [INF] * N
distB[B] = 0
while hq:
d,v = heappop(hq)
if d > distB[v]: continue
for to,c in es[v]:
if distB[to] <= d+c: continue
distB[to] = d+c
heappush(hq, (d+c, to))
hq = [(0,A)]
rdistA = [INF] * N
rdistA[A] = 0
while hq:
d,v = heappop(hq)
if d > rdistA[v]: continue
for to,c in rs[v]:
if rdistA[to] <= d+c: continue
rdistA[to] = d+c
heappush(hq, (d+c, to))
hq = [(0,B)]
rdistB = [INF] * N
rdistB[B] = 0
while hq:
d,v = heappop(hq)
if d > rdistB[v]: continue
for to,c in rs[v]:
if rdistB[to] <= d+c: continue
rdistB[to] = d+c
heappush(hq, (d+c, to))
for i in range(N-2):
ans1 = rdistA[i] + distA[B] + distB[i]
ans2 = rdistB[i] + distB[A] + distA[i]
ans = min(ans1, ans2)
print(-1 if ans >= INF else ans)
prd_xxx