結果

問題 No.2477 Drifting
ユーザー LyricalMaestro
提出日時 2025-01-01 16:18:49
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,143 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 275,048 KB
最終ジャッジ日時 2025-01-01 16:19:19
合計ジャッジ時間 27,612 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 TLE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/2477

MAX_INT = 10 ** 18

def main():
    N, M = map(int, input().split())
    next_nodes = [[] for _ in range(N)]
    for _ in range(M):
        u, v, w = map(int, input().split())
        next_nodes[u - 1].append((v - 1, w))

    K = int(input())
    state_base_map = {}
    for _ in range(K):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        c -= 1
        state_base_map[(a, b, c)] = MAX_INT

    dp = [{} for _ in range(N)]
    dp[0][-1] = 0
    for s_n in range(N):
        for prev_, cost in dp[s_n].items():
            
            for next_n, w in next_nodes[s_n]:
                if (prev_, s_n, next_n) in state_base_map:
                    continue

                new_cost = w + cost
                if s_n not in dp[next_n]:
                    dp[next_n][s_n] = MAX_INT
                dp[next_n][s_n] = min(dp[next_n][s_n], new_cost)
    
    answer = MAX_INT
    for cost in dp[-1].values():
        answer = min(answer, cost)
    if answer == MAX_INT:
        print(-1)
    else:
        print(answer)




    





if __name__ == "__main__":
    main()
0