結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー neterukunneterukun
提出日時 2021-01-22 22:27:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,220 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 174,160 KB
最終ジャッジ日時 2024-12-28 02:51:38
合計ジャッジ時間 13,918 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,992 KB
testcase_01 AC 43 ms
52,608 KB
testcase_02 AC 44 ms
52,864 KB
testcase_03 AC 41 ms
53,120 KB
testcase_04 AC 41 ms
52,992 KB
testcase_05 AC 43 ms
52,736 KB
testcase_06 AC 43 ms
52,864 KB
testcase_07 AC 42 ms
53,168 KB
testcase_08 AC 141 ms
79,232 KB
testcase_09 AC 110 ms
78,272 KB
testcase_10 AC 110 ms
79,232 KB
testcase_11 AC 88 ms
78,592 KB
testcase_12 AC 138 ms
80,340 KB
testcase_13 AC 220 ms
110,208 KB
testcase_14 AC 326 ms
118,772 KB
testcase_15 AC 251 ms
114,040 KB
testcase_16 AC 238 ms
104,976 KB
testcase_17 AC 143 ms
94,520 KB
testcase_18 AC 309 ms
132,864 KB
testcase_19 AC 288 ms
123,672 KB
testcase_20 AC 326 ms
132,500 KB
testcase_21 AC 314 ms
133,500 KB
testcase_22 AC 323 ms
130,560 KB
testcase_23 AC 149 ms
90,680 KB
testcase_24 AC 122 ms
83,072 KB
testcase_25 AC 325 ms
131,696 KB
testcase_26 AC 488 ms
174,160 KB
testcase_27 AC 373 ms
146,676 KB
testcase_28 AC 278 ms
126,928 KB
testcase_29 AC 372 ms
142,060 KB
testcase_30 AC 282 ms
128,780 KB
testcase_31 AC 209 ms
101,084 KB
testcase_32 AC 319 ms
136,200 KB
testcase_33 AC 462 ms
153,256 KB
testcase_34 AC 479 ms
152,944 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 284 ms
115,328 KB
testcase_38 AC 369 ms
108,672 KB
testcase_39 AC 353 ms
108,732 KB
testcase_40 AC 350 ms
108,748 KB
testcase_41 AC 358 ms
108,320 KB
testcase_42 AC 356 ms
108,544 KB
testcase_43 AC 376 ms
168,820 KB
testcase_44 AC 176 ms
114,960 KB
testcase_45 AC 295 ms
157,000 KB
testcase_46 WA -
testcase_47 AC 43 ms
52,736 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