結果
問題 | No.2569 はじめてのおつかいHard |
ユーザー | prd_xxx |
提出日時 | 2023-12-02 16:36:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 731 ms / 2,000 ms |
コード長 | 1,403 bytes |
コンパイル時間 | 333 ms |
コンパイル使用メモリ | 82,528 KB |
実行使用メモリ | 143,352 KB |
最終ジャッジ日時 | 2024-09-26 20:30:46 |
合計ジャッジ時間 | 5,970 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 253 ms
106,360 KB |
testcase_01 | AC | 257 ms
106,948 KB |
testcase_02 | AC | 249 ms
106,560 KB |
testcase_03 | AC | 253 ms
106,624 KB |
testcase_04 | AC | 248 ms
106,732 KB |
testcase_05 | AC | 605 ms
142,448 KB |
testcase_06 | AC | 731 ms
127,232 KB |
testcase_07 | AC | 374 ms
141,532 KB |
testcase_08 | AC | 572 ms
143,352 KB |
testcase_09 | AC | 441 ms
118,500 KB |
testcase_10 | AC | 37 ms
52,992 KB |
testcase_11 | AC | 37 ms
52,992 KB |
ソースコード
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)