結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 👑 KazunKazun
提出日時 2021-01-20 04:48:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 947 ms / 2,500 ms
コード長 2,246 bytes
コンパイル時間 1,697 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 192,252 KB
最終ジャッジ日時 2023-08-28 10:59:17
合計ジャッジ時間 25,094 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,624 KB
testcase_01 AC 93 ms
71,800 KB
testcase_02 AC 96 ms
71,696 KB
testcase_03 AC 95 ms
72,264 KB
testcase_04 AC 95 ms
72,428 KB
testcase_05 AC 94 ms
71,300 KB
testcase_06 AC 97 ms
72,204 KB
testcase_07 AC 98 ms
71,916 KB
testcase_08 AC 236 ms
86,204 KB
testcase_09 AC 163 ms
79,792 KB
testcase_10 AC 217 ms
82,664 KB
testcase_11 AC 205 ms
84,120 KB
testcase_12 AC 221 ms
82,672 KB
testcase_13 AC 679 ms
160,544 KB
testcase_14 AC 766 ms
138,864 KB
testcase_15 AC 757 ms
150,484 KB
testcase_16 AC 596 ms
124,172 KB
testcase_17 AC 409 ms
142,796 KB
testcase_18 AC 911 ms
186,220 KB
testcase_19 AC 924 ms
181,128 KB
testcase_20 AC 895 ms
180,936 KB
testcase_21 AC 925 ms
186,060 KB
testcase_22 AC 947 ms
186,400 KB
testcase_23 AC 380 ms
114,596 KB
testcase_24 AC 197 ms
86,920 KB
testcase_25 AC 567 ms
129,720 KB
testcase_26 AC 745 ms
148,060 KB
testcase_27 AC 542 ms
123,272 KB
testcase_28 AC 473 ms
115,940 KB
testcase_29 AC 500 ms
121,856 KB
testcase_30 AC 455 ms
115,224 KB
testcase_31 AC 398 ms
118,036 KB
testcase_32 AC 404 ms
121,188 KB
testcase_33 AC 737 ms
140,620 KB
testcase_34 AC 802 ms
146,268 KB
testcase_35 AC 881 ms
168,888 KB
testcase_36 AC 891 ms
160,300 KB
testcase_37 AC 361 ms
120,252 KB
testcase_38 AC 343 ms
93,576 KB
testcase_39 AC 339 ms
92,476 KB
testcase_40 AC 340 ms
92,788 KB
testcase_41 AC 338 ms
93,480 KB
testcase_42 AC 340 ms
93,484 KB
testcase_43 AC 393 ms
181,864 KB
testcase_44 AC 351 ms
159,448 KB
testcase_45 AC 384 ms
192,252 KB
testcase_46 AC 206 ms
125,604 KB
testcase_47 AC 93 ms
71,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#SCC参考元:https://judge.yosupo.jp/submission/22487
def scc(E):
    n = len(E)
    iE = [[] for _ in range(n)]
    for i, e in enumerate(E):
        for v in e:
            iE[v].append(i)
    T = []
    done = [0] * n # 0 -> 1 -> 2
    ct = 0
    for i0 in range(n):
        if done[i0]: continue
        Q = [~i0, i0]
        while Q:
            i = Q.pop()
            if i < 0:
                if done[~i] == 2: continue
                done[~i] = 2
                T.append(~i)
                ct += 1
                continue
            if i >= 0:
                if done[i]: continue
                done[i] = 1
            for j in E[i]:
                if done[j]: continue
                Q.append(~j)
                Q.append(j)

    done = [0] * n
    SCC = []
    for i0 in T[::-1]:
        if done[i0]: continue
        L = []
        Q = [~i0, i0]
        while Q:
            i = Q.pop()
            if i < 0:
                if done[~i] == 2: continue
                done[~i] = 2
                L.append(~i)
                continue
            if i >= 0:
                if done[i]: continue
                done[i] = 1
            for j in iE[i]:
                if done[j]: continue
                Q.append(~j)
                Q.append(j)
        SCC.append(L)
    return SCC
#================================================
from collections import defaultdict

N,M=map(int,input().split())
Mod=10**9+7

X=[[] for _ in range(N+1)]
E=[defaultdict(lambda :[0,0]) for _ in range(N+1)]

for _ in range(M):
    u,v,w,a=map(int,input().split())

    X[u].append(v)

    b,l=E[u][v]
    E[u][v]=[b+a,(l+a*w)%Mod]

S=scc(X)

inf=float("inf")
Flag=[0]*(N+1)
Flag[0]=1

DP=[[0,0] for _ in range(N+1)]
DP[0]=[1,0]

for U in S:
    if len(U)>=2:
        F=0
        for v in U:
            F|=Flag[v]

        if F:
            for v in U:
                DP[v]=[inf,inf]

    for u in U:
        x,y=DP[u]
        for v in E[u]:
            Flag[v]|=Flag[u]

            if x==inf:
                DP[v]=[inf,inf]
            else:
                a,w=E[u][v]
                DP[v][0]+=x*a
                DP[v][1]+=y*a+x*w

                DP[v][0]%=Mod
                DP[v][1]%=Mod

print(DP[N][1] if DP[N][1]<inf else "INF")
0