結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー mkawa2mkawa2
提出日時 2021-10-29 16:24:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,015 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 143,828 KB
最終ジャッジ日時 2024-10-07 08:37:08
合計ジャッジ時間 44,637 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
11,008 KB
testcase_01 AC 33 ms
11,008 KB
testcase_02 AC 33 ms
11,264 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 515 ms
45,708 KB
testcase_24 AC 285 ms
25,216 KB
testcase_25 AC 746 ms
50,684 KB
testcase_26 AC 1,140 ms
67,864 KB
testcase_27 AC 860 ms
55,680 KB
testcase_28 AC 557 ms
41,068 KB
testcase_29 AC 798 ms
55,552 KB
testcase_30 AC 563 ms
41,132 KB
testcase_31 AC 558 ms
45,608 KB
testcase_32 AC 637 ms
46,464 KB
testcase_33 AC 1,035 ms
64,512 KB
testcase_34 AC 1,126 ms
69,244 KB
testcase_35 AC 1,566 ms
99,644 KB
testcase_36 AC 1,508 ms
87,568 KB
testcase_37 AC 666 ms
42,752 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 1,031 ms
102,696 KB
testcase_44 AC 868 ms
91,584 KB
testcase_45 AC 527 ms
52,408 KB
testcase_46 AC 487 ms
58,680 KB
testcase_47 AC 31 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

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
s = utog[0]
t = utog[n]
# 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(s, to2)
r2 = reach(t, ot2)
# print(r1, r2)

rem = [False]*gn
for g in range(gn):
    if r1[g] and r2[g]:
        if len(gg[g]) > 1:
            print("INF")
            exit()
    else:
        rem[g] = True

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

for g in range(t, s-1, -1):
    if rem[g]: continue
    for h, l, a in to2[g]:
        if rem[h]: continue
        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