結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-22 22:47:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,622 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 86,888 KB
実行使用メモリ 142,856 KB
最終ジャッジ日時 2023-08-27 21:02:47
合計ジャッジ時間 15,949 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,740 KB
testcase_01 AC 87 ms
71,520 KB
testcase_02 AC 89 ms
71,740 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 206 ms
94,980 KB
testcase_24 AC 161 ms
92,208 KB
testcase_25 AC 353 ms
116,312 KB
testcase_26 AC 464 ms
132,724 KB
testcase_27 AC 334 ms
116,172 KB
testcase_28 AC 277 ms
106,368 KB
testcase_29 AC 329 ms
115,348 KB
testcase_30 AC 283 ms
106,756 KB
testcase_31 AC 241 ms
100,080 KB
testcase_32 AC 265 ms
111,176 KB
testcase_33 AC 439 ms
129,544 KB
testcase_34 AC 486 ms
130,276 KB
testcase_35 AC 360 ms
128,044 KB
testcase_36 AC 290 ms
118,364 KB
testcase_37 AC 238 ms
110,996 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 247 ms
138,428 KB
testcase_44 AC 218 ms
112,400 KB
testcase_45 AC 248 ms
130,348 KB
testcase_46 AC 97 ms
79,580 KB
testcase_47 AC 89 ms
71,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10**9+7

N, M = map(int, input().split())
go = [[] for _ in range(N+1)]
back = [[] for _ in range(N+1)]
for _ in range(M):
    a, b, c, d = map(int, input().split())
    go[a].append((b, c, d))
    back[b].append((a, c, d))


# 0から到達可能なノードを調べる
# 0-Nが非連結なら0
# 到達可能内でサイクルがあればINF
vis0 = [0] * (N + 1)
vis0[0] = 1
q = [0]
while q:
    s = q.pop()
    for t, _, _ in go[s]:
        if not vis0[t]:
            vis0[t] = 1
            q.append(t)

visN = [0] * (N + 1)
visN[N] = 1
q = [N]
while q:
    s = q.pop()
    for t, _, _ in back[s]:
        if not visN[t]:
            visN[t] = 1
            q.append(t)

ok = set(i for i in range(N + 1) if vis0[i] and visN[i])
if not N in ok:
    print(0)
    exit()

deg = [0] * (N+1)
for s, e in enumerate(go):
    if s in ok:
        for t, _, _ in e:
            if t in ok:
                deg[t] += 1
q = [i for i, d in enumerate(deg) if i in ok and d == 0]
topo = []
while q:
    s = q.pop()
    topo.append(s)
    for t, _, _ in go[s]:
        if t in ok:
            deg[t] -= 1
            if deg[t] == 0:
                q.append(t)

if len(topo) < len(ok):
    print("INF")
    exit()

cnt = [0] * (N + 1)
cnt[N] = 1
for s in topo[::-1]:
    for t, _, num in back[s]:
        if t in ok:
            cnt[t] += cnt[s] * num
            cnt[t] %= mod

ans = 0
for s in topo:
    for t, L, num in go[s]:
        if t in ok:
            ans += L * num * cnt[t]
            ans %= mod

print(ans)
0