結果
問題 | No.2569 はじめてのおつかいHard |
ユーザー |
![]() |
提出日時 | 2023-12-02 16:22:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 705 ms / 2,000 ms |
コード長 | 1,315 bytes |
コンパイル時間 | 356 ms |
コンパイル使用メモリ | 82,848 KB |
実行使用メモリ | 120,832 KB |
最終ジャッジ日時 | 2024-09-26 20:12:21 |
合計ジャッジ時間 | 5,976 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 10 |
ソースコード
#############################################################import syssys.setrecursionlimit(10**7)from heapq import heappop,heappushfrom collections import deque,defaultdict,Counterfrom bisect import bisect_left, bisect_rightfrom itertools import product,combinations,permutationsfrom math import sin,cos#from math import isqrt #DO NOT USE PyPyipt = sys.stdin.readlineiin = lambda :int(ipt())lmin = lambda :list(map(int,ipt().split()))INF = 1<<60def dijkstra_r(g,s,n):cost = [INF] * nhq = []heappush(hq,(0,s))while hq:c1,v1 = heappop(hq)if cost[v1] < c1:continuecost[v1] = c1for v2,c2 in g[v1]:if c1+c2 < cost[v2]:cost[v2] = c1+c2heappush(hq,(cost[v2],v2))return cost#############################################################N,M = lmin()G = [[] for _ in range(N)]RG = [[] for _ in range(N)]for _ in range(M):a,b,t = lmin()G[a-1].append((b-1,t))RG[b-1].append((a-1,t))cost2 = dijkstra_r(G,N-2,N)cost2r = dijkstra_r(RG,N-2,N)cost3 = dijkstra_r(G,N-1,N)cost3r = dijkstra_r(RG,N-1,N)for i in range(N-2):ans = min(cost2r[i]+cost2[N-1]+cost3[i],cost3r[i]+cost3[N-2]+cost2[i])if ans >= INF:print(-1)else:print(ans)