結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー mkawa2mkawa2
提出日時 2021-10-29 16:39:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,999 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 143,832 KB
最終ジャッジ日時 2024-10-07 08:38:45
合計ジャッジ時間 40,634 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,264 KB
testcase_01 AC 28 ms
11,264 KB
testcase_02 AC 28 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 436 ms
45,576 KB
testcase_24 AC 238 ms
25,344 KB
testcase_25 AC 649 ms
50,684 KB
testcase_26 AC 1,010 ms
67,860 KB
testcase_27 AC 764 ms
55,680 KB
testcase_28 AC 468 ms
41,200 KB
testcase_29 AC 719 ms
55,680 KB
testcase_30 AC 476 ms
41,140 KB
testcase_31 AC 464 ms
45,476 KB
testcase_32 AC 569 ms
46,720 KB
testcase_33 AC 949 ms
64,640 KB
testcase_34 AC 1,010 ms
69,236 KB
testcase_35 AC 1,447 ms
99,764 KB
testcase_36 AC 1,310 ms
87,820 KB
testcase_37 AC 603 ms
42,880 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 979 ms
102,692 KB
testcase_44 AC 803 ms
91,576 KB
testcase_45 AC 483 ms
52,536 KB
testcase_46 AC 443 ms
58,596 KB
testcase_47 AC 28 ms
11,264 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
# 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

s = utog[0]
t = utog[n]
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

dd = [0]*gn
cc = [0]*gn
cc[t] = 1

for g in range(t-1, 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
        dd[g] += c*l%md+dd[h]
        cc[g] %= md
        dd[g] %= md

print(dd[s])
0