結果

問題 No.3393 Move on Highway
コンテスト
ユーザー 回転
提出日時 2025-12-01 22:46:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,102 ms / 3,000 ms
コード長 1,468 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 324 ms
コンパイル使用メモリ 82,120 KB
実行使用メモリ 145,736 KB
最終ジャッジ日時 2025-12-01 22:47:26
合計ジャッジ時間 46,062 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def main():
    import sys
    input = sys.stdin.readline
    import heapq
    N,M,C = list(map(int,input().split()))
    edge = [[] for _ in range(N)]
    for _ in range(M):
        u,v,w = list(map(int,input().split()))
        u -= 1;v -= 1
        edge[u].append((v,w+C))
        edge[v].append((u,w+C))

    def make_hash(a,b):
        return (a<<20) + b
    
    def read_hash(x):
        return x>>20, x & ((1<<20) - 1)

    q = [make_hash(0,0)]
    INF = 10**18
    from_0 =[INF for _ in range(N)]
    while(q):
        v,now = read_hash(heapq.heappop(q))

        if(from_0[now] <= v):continue
        from_0[now] = v

        for i,w in edge[now]:
            if(from_0[i] > v+w):heapq.heappush(q,make_hash(v+w,i))

    def make_hash2(a,b,c):
        ret = a<<21
        ret |= b<<1
        ret |= c
        return ret
    
    def read_hash2(x):
        return x>>21, (x>>1) & ((1<<20)-1), x & 1

    q = [make_hash2(0,N-1,0)]
    from_N = [[INF,INF] for _ in range(N)]
    while(q):
        v,now,state = read_hash2(heapq.heappop(q))

        if(from_N[now][state] <= v):continue
        from_N[now][state] = v

        for i,w in edge[now]:
            if(from_N[i][state] > v+w):heapq.heappush(q,make_hash2(v+w,i,state))
            if(state == 0 and from_N[i][1] > v+C):heapq.heappush(q,make_hash2(v+C,i,1))

    ans = []
    for i in range(1,N):
        ans.append(min(from_0[-1],from_0[i] + min(from_N[i])))

    print("\n".join(map(str,ans)))

main()
0