結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー neterukunneterukun
提出日時 2021-01-22 22:16:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,031 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 175,104 KB
最終ジャッジ日時 2023-08-27 19:41:48
合計ジャッジ時間 15,625 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,464 KB
testcase_01 AC 77 ms
71,380 KB
testcase_02 AC 77 ms
71,496 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 185 ms
91,968 KB
testcase_24 AC 160 ms
87,444 KB
testcase_25 AC 384 ms
143,604 KB
testcase_26 AC 527 ms
175,104 KB
testcase_27 AC 400 ms
143,628 KB
testcase_28 AC 302 ms
129,152 KB
testcase_29 AC 385 ms
141,520 KB
testcase_30 AC 313 ms
125,384 KB
testcase_31 AC 224 ms
100,148 KB
testcase_32 AC 331 ms
134,304 KB
testcase_33 AC 507 ms
152,028 KB
testcase_34 AC 508 ms
156,568 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 307 ms
113,512 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 358 ms
169,320 KB
testcase_44 AC 207 ms
116,516 KB
testcase_45 AC 310 ms
157,752 KB
testcase_46 WA -
testcase_47 AC 76 ms
71,416 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(-1)
    exit()

for i in range(1, 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()

ptn = [0] * (n + 1)
ans = [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[nxt_v] += l * a * ptn[v]
            ptn[nxt_v] %= MOD
            ans[nxt_v] %= MOD

print(sum(ans) % MOD)
0