結果
| 問題 | No.2569 はじめてのおつかいHard |
| コンテスト | |
| ユーザー |
mymelochan
|
| 提出日時 | 2023-12-02 16:22:34 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 739 ms / 2,000 ms |
| コード長 | 1,315 bytes |
| 記録 | |
| コンパイル時間 | 186 ms |
| コンパイル使用メモリ | 82,696 KB |
| 実行使用メモリ | 128,768 KB |
| 最終ジャッジ日時 | 2025-06-20 01:55:40 |
| 合計ジャッジ時間 | 7,497 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 12 |
ソースコード
#############################################################
import sys
sys.setrecursionlimit(10**7)
from heapq import heappop,heappush
from collections import deque,defaultdict,Counter
from bisect import bisect_left, bisect_right
from itertools import product,combinations,permutations
from math import sin,cos
#from math import isqrt #DO NOT USE PyPy
ipt = sys.stdin.readline
iin = lambda :int(ipt())
lmin = lambda :list(map(int,ipt().split()))
INF = 1<<60
def dijkstra_r(g,s,n):
cost = [INF] * n
hq = []
heappush(hq,(0,s))
while hq:
c1,v1 = heappop(hq)
if cost[v1] < c1:
continue
cost[v1] = c1
for v2,c2 in g[v1]:
if c1+c2 < cost[v2]:
cost[v2] = c1+c2
heappush(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)
mymelochan