結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
コンテスト
ユーザー norioc
提出日時 2026-10-04 21:57:09
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,101 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 70 ms
コンパイル使用メモリ 80,512 KB
実行使用メモリ 263,948 KB
最終ジャッジ日時 2026-10-04 21:57:33
合計ジャッジ時間 21,758 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict
from atcoder.scc import SCCGraph


def scc(n: int, adj: dict[int, list[int]]) -> list[list[int]]:
    """グラフを強連結成分分解する。

    Args:
        n: 頂点数
        adj: 隣接リスト {u: [v...]}

    Returns:
        強連結成分のリスト (トポロジカル順)
    """

    g = SCCGraph(n)
    for u, vs in adj.items():
        for v in vs:
            g.add_edge(u, v)

    return g.scc()


MOD = 10**9 + 7
N, M = map(int, input().split())

adj = defaultdict(list)
adj2 = defaultdict(list)
for _ in range(M):
    u, v, l, a = map(int, input().split())
    adj[u].append(v)
    adj2[u].append((v, l, a))


cnts = [0] * (N+1)
tots = [0] * (N+1)

cnts[0] = 1

b = False
cc = scc(N+1, adj)
for g in cc:
    if 0 in g:
        b = True
    if not b: continue

    if len(g) > 1:
        print('INF')
        exit()

    for v in g:
        for to, l, a in adj2[v]:
            tots[to] += (tots[v] + cnts[v] * l) * a
            tots[to] %= MOD
            cnts[to] += cnts[v] * a
            cnts[to] %= MOD

ans = tots[N]
print(ans)
0