結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー neterukunneterukun
提出日時 2021-01-22 22:29:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 506 ms / 2,500 ms
コード長 2,215 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 86,732 KB
実行使用メモリ 174,640 KB
最終ジャッジ日時 2023-08-28 11:03:43
合計ジャッジ時間 15,273 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,296 KB
testcase_01 AC 74 ms
71,080 KB
testcase_02 AC 73 ms
71,080 KB
testcase_03 AC 72 ms
71,044 KB
testcase_04 AC 78 ms
70,900 KB
testcase_05 AC 74 ms
71,060 KB
testcase_06 AC 74 ms
71,280 KB
testcase_07 AC 73 ms
71,348 KB
testcase_08 AC 121 ms
80,048 KB
testcase_09 AC 135 ms
79,204 KB
testcase_10 AC 136 ms
79,876 KB
testcase_11 AC 117 ms
79,484 KB
testcase_12 AC 167 ms
81,408 KB
testcase_13 AC 271 ms
117,892 KB
testcase_14 AC 366 ms
119,256 KB
testcase_15 AC 288 ms
113,428 KB
testcase_16 AC 279 ms
107,592 KB
testcase_17 AC 190 ms
105,464 KB
testcase_18 AC 339 ms
134,288 KB
testcase_19 AC 336 ms
133,828 KB
testcase_20 AC 347 ms
134,472 KB
testcase_21 AC 354 ms
133,972 KB
testcase_22 AC 362 ms
134,428 KB
testcase_23 AC 186 ms
91,868 KB
testcase_24 AC 155 ms
87,256 KB
testcase_25 AC 369 ms
143,528 KB
testcase_26 AC 506 ms
174,640 KB
testcase_27 AC 394 ms
143,792 KB
testcase_28 AC 293 ms
128,844 KB
testcase_29 AC 367 ms
141,216 KB
testcase_30 AC 297 ms
125,324 KB
testcase_31 AC 216 ms
100,240 KB
testcase_32 AC 325 ms
133,884 KB
testcase_33 AC 484 ms
151,632 KB
testcase_34 AC 501 ms
157,552 KB
testcase_35 AC 248 ms
105,692 KB
testcase_36 AC 226 ms
104,432 KB
testcase_37 AC 298 ms
113,284 KB
testcase_38 AC 362 ms
110,900 KB
testcase_39 AC 359 ms
110,736 KB
testcase_40 AC 362 ms
111,008 KB
testcase_41 AC 373 ms
110,852 KB
testcase_42 AC 361 ms
110,628 KB
testcase_43 AC 375 ms
168,984 KB
testcase_44 AC 196 ms
116,884 KB
testcase_45 AC 290 ms
157,660 KB
testcase_46 AC 81 ms
78,500 KB
testcase_47 AC 74 ms
71,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline


def topological_sorted(digraph):
    n = len(digraph)
    indegree = [0] * n
    for v in range(n):
        for nxt_v in digraph[v]:
            indegree[nxt_v] += 1

    tp_order = [i for i in range(n) if indegree[i] == 0]
    stack = tp_order[:]
    while stack:
        v = stack.pop()
        for nxt_v in digraph[v]:
            indegree[nxt_v] -= 1
            if indegree[nxt_v] == 0:
                stack.append(nxt_v)
                tp_order.append(nxt_v)

    return len(tp_order) == n, tp_order


n, m = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(m)]
MOD = 10 ** 9 + 7


graph = [[] for i in range(n + 1)]
for u, v, _, _ in edges:
    graph[u].append(v)
rev_graph = [[] for i in range(n + 1)]
for u, v, _, _ in edges:
    rev_graph[v].append(u)

root = 0
reach = [False] * (n + 1)
reach[0] = True
stack = [root]
while stack:
    v = stack.pop()
    for nxt_v in graph[v]:
        if reach[nxt_v]:
            continue
        reach[nxt_v] = True
        stack.append(nxt_v)

root = n
rev_reach = [False] * (n + 1)
rev_reach[n] = True
stack = [root]
while stack:
    v = stack.pop()
    for nxt_v in rev_graph[v]:
        if rev_reach[nxt_v]:
            continue
        rev_reach[nxt_v] = True
        stack.append(nxt_v)

if not reach[n]:
    print(0)
    exit()

for i in range(n + 1):
    reach[i] = reach[i] & rev_reach[i]

graph = [set() for i in range(n + 1)]
memo = {}
for u, v, l, a in edges:
    if reach[u] and reach[v]:
        graph[u].add(v)
        if (u, v) not in memo:
            memo[u, v] = []
        memo[u, v].append((l, a))

flag, tp_order = topological_sorted(graph)
if not flag:
    print("INF")
    exit()

    

nxt_ptn = [0] * (n + 1)
nxt_ptn[n] = 1
for v in tp_order[::-1]:
    for nxt_v in graph[v]:
        for l, a in memo[v, nxt_v]:
            nxt_ptn[v] += nxt_ptn[nxt_v] * a
            nxt_ptn[v] %= MOD

ans = 0
ptn = [0] * (n + 1)
ptn[0] = 1
for v in tp_order:
    for nxt_v in graph[v]:
        for l, a in memo[v, nxt_v]:
            ptn[nxt_v] += ptn[v] * a
            ans += ptn[v] * nxt_ptn[nxt_v] * l * a
            ptn[nxt_v] %= MOD
            ans %= MOD
print(ans)
0