結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー mkawa2mkawa2
提出日時 2021-10-29 16:13:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,992 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 219,012 KB
最終ジャッジ日時 2024-04-16 13:23:45
合計ジャッジ時間 26,818 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,864 KB
testcase_01 AC 41 ms
52,608 KB
testcase_02 AC 43 ms
52,736 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 356 ms
112,520 KB
testcase_24 AC 157 ms
90,880 KB
testcase_25 AC 468 ms
115,712 KB
testcase_26 AC 620 ms
133,888 KB
testcase_27 AC 459 ms
122,496 KB
testcase_28 AC 376 ms
103,552 KB
testcase_29 AC 438 ms
123,008 KB
testcase_30 AC 368 ms
103,936 KB
testcase_31 AC 338 ms
103,680 KB
testcase_32 AC 333 ms
114,944 KB
testcase_33 AC 597 ms
132,608 KB
testcase_34 AC 664 ms
131,552 KB
testcase_35 AC 789 ms
161,384 KB
testcase_36 AC 695 ms
142,764 KB
testcase_37 AC 300 ms
122,112 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 371 ms
167,212 KB
testcase_44 AC 342 ms
165,072 KB
testcase_45 AC 241 ms
125,696 KB
testcase_46 AC 114 ms
104,092 KB
testcase_47 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (1, 0), (0, -1), (-1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 1 << 63
# md = 998244353
md = 10**9+7

# 各強連結成分をリストにしてトポロジカル順に返す
def SCC(to, ot):
    n = len(to)
    # トポロジカルソート
    fin = [-1]*n
    topo = []
    for u in range(n):
        if fin[u] != -1: continue
        stack = [u]
        while stack:
            u = stack[-1]
            if fin[u] == -1:
                fin[u] = 0
                for v, _, _ in to[u]:
                    if fin[v] != -1: continue
                    stack.append(v)
            else:
                stack.pop()
                if fin[u] == 0:
                    fin[u] = 1
                    topo.append(u)
    # 逆辺でdfs
    res = []
    while topo:
        u = topo.pop()
        if fin[u] != 1: continue
        fin[u] = 2
        cur = [u]
        i = 0
        while i < len(cur):
            u = cur[i]
            for v, _, _ in ot[u]:
                if fin[v] == 2: continue
                fin[v] = 2
                cur.append(v)
            i += 1
        res.append(cur)

    return res

n, m = LI()
to = [[] for _ in range(n+1)]
ot = [[] for _ in range(n+1)]
for i in range(m):
    u, v, l, a = LI()
    to[u].append((v, l, a))
    ot[v].append((u, l, a))

gg = SCC(to, ot)
gn = len(gg)
# print(gg)

utog = [-1]*(n+1)
for g, uu in enumerate(gg):
    for u in uu:
        utog[u] = g
# print(utog)

to2 = [[] for _ in range(gn)]
ot2 = [[] for _ in range(gn)]
for u in range(n+1):
    g = utog[u]
    for v, l, a in to[u]:
        h = utog[v]
        if g == h: continue
        to2[g].append((h, l, a))
        ot2[h].append((g, l, a))
# print(to2, ot2)

def reach(g, to):
    res = [False]*gn
    res[g] = True
    stack = [g]
    while stack:
        g = stack.pop()
        for h, _, _ in to[g]:
            if res[h]: continue
            res[h] = True
            stack.append(h)
    return res

r1 = reach(utog[0], to2)
r2 = reach(utog[n], ot2)
# print(r1, r2)

cnt = []
for c0, c1 in zip(r1, r2):
    cnt.append(c0+c1)

for g in range(gn):
    if cnt[g] < 2: continue
    if len(gg[g]) > 1:
        print("INF")
        exit()

ss = [0]*gn
cc = [0]*gn
g = utog[n]
cc[g] = 1

for g in range(utog[n], utog[0]-1, -1):
    if cnt[g] < 2: continue
    for h, l, a in to[g]:
        c = cc[h]*a%md
        cc[g] += c
        ss[g] += c*l%md+ss[h]
        cc[g] %= md
        ss[g] %= md

print(ss[utog[0]])
0