結果

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

ソースコード

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
    a,b=v,last[v]
    if a>b:
        a,b=b,a
    G[v][last[v]]=nextc[(a,b)]
    G[last[v]][v]=nextc[(a,b)]
    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
    if u>v:
        v,u=u,v
    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