結果

問題 No.3561 Collect KCPC
コンテスト
ユーザー kidodesu
提出日時 2026-05-29 20:09:55
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 2,935 ms / 6,000 ms
コード長 1,380 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 525 ms
コンパイル使用メモリ 85,504 KB
実行使用メモリ 210,068 KB
最終ジャッジ日時 2026-05-29 20:10:29
合計ジャッジ時間 31,458 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点1 10 % AC * 15
部分点2 20 % AC * 15
部分点3 20 % AC * 13
部分点4 50 % AC * 51
合計 100 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from heapq import *
def main():
    n, m = list(map(int, input().split()))
    node = [[] for _ in range(n)]
    for _ in range(m):
        u, v, w = list(map(int, input().split()))
        node[u-1].append((v-1, w))
    S = input()
    inf = 1 << 60
    dp = [[inf for _ in range(4)] for _ in range(n)]
    dp[0][0] = 0
    ep = [set() for _ in range(2*n)]
    hq = []
    heappush(hq, (0, 0, 0, -1))
    while hq:
        c, now, f, pre = heappop(hq)
        if f < 2 and dp[now][f] < c: continue
        if 2 <= f and (pre in ep[2*now+f-2] or 2 <= len(ep[2*now+f-2])): continue
        if 2 <= f: ep[2*now+f-2].add(pre)
        if S[now] == "KCPC"[f]:
            if f == 0:
                if dp[now][1] > c:
                    dp[now][1] = c
                    heappush(hq, (c, now, 1, pre))
            elif f == 1:
                dp[now][2] = min(dp[now][2], c)
                heappush(hq, (c, now, 2, now))
            elif f == 2:
                dp[now][3] = min(dp[now][3], c)
                heappush(hq, (c, now, 3, pre))
            elif pre != now: return c
        for nxt, w in node[now]:
            if f < 2 and dp[nxt][f] > c+w:
                dp[nxt][f] = c+w
                heappush(hq, (c+w, nxt, f, pre))
            else:
                dp[nxt][f] = min(dp[nxt][f], c+w)
                heappush(hq, (c+w, nxt, f, pre))
    return -1

print(main())
0