結果

問題 No.1301 Strange Graph Shortest Path
ユーザー proribone
提出日時 2020-11-27 22:44:45
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 896 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 125,004 KB
最終ジャッジ日時 2024-07-26 20:03:42
合計ジャッジ時間 21,296 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other WA * 1 RE * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush,heappop,heapify
INF=float('inf')

def dijkstra(G,s,n):
    que=[(0,s)]
    dist=[INF]*n
    last=[-1 for i in range(n)]
    dist[s]=0
    while que:
        mincost,u=heappop(que)
        if(mincost>dist[u]):
            continue
        for v,c in G[u].items():
            if(dist[u]+c<dist[v]):
                dist[v]=dist[u]+c
                last[v]=u
                heappush(que,(dist[v],v))
    return dist,last
    
def update(v):
    if last[v]==-1:
        return
    G[v][last[v]]=nextc[(last[v],v)]
    G[last[v]][v]=nextc[(last[v],v)]
    update(last[v])
    

N,M=map(int,input().split())

G=[{} for _ in range(N)]
nextc={}

for _ in range(M):
    u,v,c,d=map(int,input().split())
    u-=1
    v-=1
    G[u][v]=c
    G[v][u]=c
    nextc[(u,v)]=d
    
dist,last=dijkstra(G,0,N)
ans=dist[-1]
update(N-1)
dist,last=dijkstra(G,N-1,N)
ans+=dist[0]
print(ans)
0