結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー H3PO4H3PO4
提出日時 2021-01-22 22:51:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,586 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 11,088 KB
実行使用メモリ 168,352 KB
最終ジャッジ日時 2023-08-27 21:19:33
合計ジャッジ時間 44,918 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 743 ms
42,920 KB
testcase_01 AC 200 ms
42,788 KB
testcase_02 AC 198 ms
42,948 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 383 ms
62,708 KB
testcase_24 AC 397 ms
60,436 KB
testcase_25 AC 1,074 ms
120,448 KB
testcase_26 AC 1,703 ms
168,352 KB
testcase_27 AC 1,213 ms
134,656 KB
testcase_28 AC 812 ms
101,160 KB
testcase_29 AC 1,177 ms
133,700 KB
testcase_30 AC 843 ms
104,428 KB
testcase_31 AC 510 ms
76,548 KB
testcase_32 AC 962 ms
116,368 KB
testcase_33 AC 1,580 ms
163,340 KB
testcase_34 AC 1,453 ms
146,844 KB
testcase_35 AC 819 ms
102,744 KB
testcase_36 AC 814 ms
102,800 KB
testcase_37 AC 838 ms
96,868 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 1,189 ms
139,040 KB
testcase_44 AC 595 ms
77,016 KB
testcase_45 AC 1,056 ms
139,188 KB
testcase_46 AC 216 ms
43,740 KB
testcase_47 AC 194 ms
82,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict, deque
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix

input = sys.stdin.buffer.readline


def topological_sort(N, edges):
    outs = defaultdict(list)
    ins = defaultdict(int)
    for v1, v2, in edges:
        outs[v1].append(v2)
        ins[v2] += 1

    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    while q:
        v1 = q.popleft()
        yield v1
        for v2 in outs[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                q.append(v2)


MOD = 10 ** 9 + 7
N, M = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(M)]

# 強連結成分分解により不要部分を削除
edges_set = set((u, v) for u, v, _, _ in edges)
edges_set.add((N, 0))
frm, to = [], []
for u, v in edges_set:
    frm.append(u)
    to.append(v)
matr = csr_matrix(([1] * len(frm), (frm, to)), shape=(N + 1, N + 1))
_, labels = connected_components(matr, connection='strong')
l0 = labels[0]
nodes = [i for i, x in enumerate(labels) if x == l0]

# 座圧
comp = {x: i for i, x in enumerate(nodes)}
N1 = len(comp)
succ = [[] for _ in range(N1)]
new_edges = set()
for u, v, l, a in edges:
    if u in comp and v in comp:
        cu, cv = comp[u], comp[v]
        succ[cu].append((cv, l, a))
        new_edges.add((cu, cv))

# トポソ
ts = list(topological_sort(N1, new_edges))
if len(ts) != N1: print('INF');exit()

dp = [0] * N1
for v in ts:
    cur = dp[v]
    for s, l, a in succ[v]:
        dp[s] += (cur + l) * a
        dp[s] %= MOD
print(dp[ts[-1]])
0