結果

問題 No.1301 Strange Graph Shortest Path
ユーザー ShirotsumeShirotsume
提出日時 2023-02-17 21:01:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,580 bytes
コンパイル時間 428 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 154,700 KB
最終ジャッジ日時 2023-09-26 18:09:42
合計ジャッジ時間 31,444 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,372 KB
testcase_01 AC 91 ms
71,456 KB
testcase_02 WA -
testcase_03 AC 696 ms
137,160 KB
testcase_04 AC 905 ms
152,008 KB
testcase_05 AC 695 ms
148,972 KB
testcase_06 AC 814 ms
144,276 KB
testcase_07 AC 793 ms
146,076 KB
testcase_08 AC 659 ms
135,908 KB
testcase_09 AC 787 ms
141,452 KB
testcase_10 WA -
testcase_11 AC 855 ms
148,508 KB
testcase_12 AC 873 ms
151,088 KB
testcase_13 AC 787 ms
150,792 KB
testcase_14 AC 770 ms
140,020 KB
testcase_15 AC 776 ms
140,104 KB
testcase_16 AC 908 ms
152,860 KB
testcase_17 AC 803 ms
149,500 KB
testcase_18 AC 745 ms
138,468 KB
testcase_19 AC 843 ms
149,788 KB
testcase_20 AC 815 ms
145,864 KB
testcase_21 AC 869 ms
151,728 KB
testcase_22 AC 831 ms
152,836 KB
testcase_23 AC 789 ms
150,936 KB
testcase_24 AC 807 ms
145,968 KB
testcase_25 AC 925 ms
150,560 KB
testcase_26 AC 833 ms
147,704 KB
testcase_27 AC 861 ms
148,956 KB
testcase_28 AC 697 ms
140,636 KB
testcase_29 WA -
testcase_30 AC 894 ms
150,840 KB
testcase_31 AC 892 ms
152,004 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 834 ms
154,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque, Counter
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353

def Dijkstra(s, graph):
    INF = 2 ** 63 - 1
    import heapq
    n = len(graph)
    dist = [INF] * n
    dist[s] = 0
    bef = [0] * n
    bef[s] = s
    hq = [(0, s)]
    heapq.heapify(hq)
    while hq:
        c, now = heapq.heappop(hq)
        
        if c > dist[now]:
            continue
        for to, cost in graph[now]:
            if dist[now] + cost < dist[to]:
                dist[to] = cost + dist[now]
                bef[to] = now
                heapq.heappush(hq, (dist[to], + to))
    return dist, bef

def DijkstraRest(bef, t):
    now = t
    ret = []
    while bef[now] != now:
        ret.append((bef[now], now))
        now = bef[now]
    ret.reverse()
    return ret

n, m = mi()
edge = [li() for _ in range(m)]
D = {}
for u, v, c, d in edge:
    u -= 1; v -= 1
    D[u, v] = D[v, u] = (c, d)

graph = [[] for _ in range(n)]

for V in D.keys():
    u, v = V
    graph[u].append((v, D[V][0]))
    graph[v].append((u, D[V][0]))

dist, r = Dijkstra(0, graph)

ans = dist[n - 1]

R = set(DijkstraRest(r, n - 1))

graph = [[] for _ in range(n)]

for V in D.keys():
    u, v = V
    if (u, v) in R or (v, u) in R:
        graph[u].append((v, D[V][1]))
        graph[v].append((u, D[V][1]))
    else:
        graph[u].append((v, D[V][0]))
        graph[v].append((u, D[V][0]))

dist, r = Dijkstra(0, graph)

ans += dist[n - 1]

print(ans)
0