結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー neterukunneterukun
提出日時 2021-01-22 22:27:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,220 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 175,124 KB
最終ジャッジ日時 2023-08-27 20:14:36
合計ジャッジ時間 17,172 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,060 KB
testcase_01 AC 75 ms
71,308 KB
testcase_02 AC 76 ms
71,244 KB
testcase_03 AC 76 ms
71,504 KB
testcase_04 AC 77 ms
71,228 KB
testcase_05 AC 75 ms
71,268 KB
testcase_06 AC 77 ms
71,308 KB
testcase_07 AC 78 ms
71,448 KB
testcase_08 AC 144 ms
80,304 KB
testcase_09 AC 140 ms
79,572 KB
testcase_10 AC 138 ms
80,252 KB
testcase_11 AC 121 ms
79,572 KB
testcase_12 AC 172 ms
81,884 KB
testcase_13 AC 294 ms
118,312 KB
testcase_14 AC 398 ms
119,272 KB
testcase_15 AC 308 ms
113,564 KB
testcase_16 AC 292 ms
107,816 KB
testcase_17 AC 202 ms
105,372 KB
testcase_18 AC 379 ms
134,544 KB
testcase_19 AC 384 ms
134,136 KB
testcase_20 AC 391 ms
134,436 KB
testcase_21 AC 370 ms
134,240 KB
testcase_22 AC 384 ms
134,708 KB
testcase_23 AC 195 ms
92,080 KB
testcase_24 AC 165 ms
87,632 KB
testcase_25 AC 440 ms
143,536 KB
testcase_26 AC 596 ms
175,124 KB
testcase_27 AC 481 ms
143,984 KB
testcase_28 AC 316 ms
128,712 KB
testcase_29 AC 394 ms
141,600 KB
testcase_30 AC 321 ms
125,444 KB
testcase_31 AC 232 ms
100,292 KB
testcase_32 AC 348 ms
133,896 KB
testcase_33 AC 531 ms
152,032 KB
testcase_34 AC 542 ms
157,756 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 322 ms
113,572 KB
testcase_38 AC 395 ms
110,892 KB
testcase_39 AC 404 ms
111,000 KB
testcase_40 AC 394 ms
111,148 KB
testcase_41 AC 394 ms
111,176 KB
testcase_42 AC 399 ms
110,964 KB
testcase_43 AC 415 ms
169,096 KB
testcase_44 AC 213 ms
117,052 KB
testcase_45 AC 311 ms
157,940 KB
testcase_46 WA -
testcase_47 AC 75 ms
71,052 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("INF")
    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