結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー rlangevinrlangevin
提出日時 2024-01-08 01:29:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,479 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 157,204 KB
最終ジャッジ日時 2024-01-08 01:29:50
合計ジャッジ時間 16,617 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,604 KB
testcase_01 AC 40 ms
55,604 KB
testcase_02 AC 40 ms
55,604 KB
testcase_03 AC 40 ms
55,604 KB
testcase_04 AC 43 ms
55,604 KB
testcase_05 AC 42 ms
55,604 KB
testcase_06 AC 43 ms
55,604 KB
testcase_07 AC 44 ms
55,604 KB
testcase_08 AC 107 ms
78,704 KB
testcase_09 AC 83 ms
77,400 KB
testcase_10 AC 105 ms
78,192 KB
testcase_11 AC 104 ms
78,448 KB
testcase_12 AC 97 ms
78,064 KB
testcase_13 AC 492 ms
130,884 KB
testcase_14 AC 536 ms
141,028 KB
testcase_15 AC 624 ms
142,736 KB
testcase_16 AC 394 ms
122,532 KB
testcase_17 AC 263 ms
102,848 KB
testcase_18 AC 676 ms
156,172 KB
testcase_19 AC 658 ms
156,944 KB
testcase_20 AC 691 ms
157,068 KB
testcase_21 AC 659 ms
156,800 KB
testcase_22 AC 702 ms
157,204 KB
testcase_23 AC 186 ms
95,756 KB
testcase_24 AC 119 ms
84,828 KB
testcase_25 AC 277 ms
102,076 KB
testcase_26 AC 445 ms
133,956 KB
testcase_27 AC 313 ms
120,800 KB
testcase_28 AC 220 ms
94,100 KB
testcase_29 AC 271 ms
109,192 KB
testcase_30 AC 225 ms
92,992 KB
testcase_31 AC 217 ms
98,852 KB
testcase_32 AC 243 ms
112,496 KB
testcase_33 AC 389 ms
120,376 KB
testcase_34 AC 436 ms
127,704 KB
testcase_35 WA -
testcase_36 AC 278 ms
115,580 KB
testcase_37 AC 209 ms
104,608 KB
testcase_38 AC 349 ms
137,024 KB
testcase_39 AC 334 ms
137,032 KB
testcase_40 AC 334 ms
137,028 KB
testcase_41 AC 331 ms
137,160 KB
testcase_42 AC 336 ms
137,160 KB
testcase_43 AC 214 ms
123,956 KB
testcase_44 AC 174 ms
112,132 KB
testcase_45 AC 199 ms
118,064 KB
testcase_46 AC 65 ms
84,044 KB
testcase_47 AC 41 ms
55,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from collections import deque
def topsort(G):
    Q = deque()
    N = len(G)
    count = [0] * N
    for u in range(N):
        for v, _, _ in G[u]:
            count[v] += 1

    for u in range(N):
        if count[u] == 0:
            Q.append(u)
    anslst = []
    while Q:
        u = Q.pop()
        anslst.append(u)
        for v, _, _ in G[u]:
            count[v] -= 1
            if count[v] == 0:
                Q.append(v)
    return anslst

def bfs(G, s):
    Q = deque([s])
    N = len(G)
    dist = [-1] * N
    par = [-1] * N
    dist[s] = 0
    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != -1:
                continue
            dist[v] = dist[u] + 1
            par[v] = u
            Q.append(v)
    return dist

N, M = map(int, input().split())
G = [[] for i in range(N + 1)]
Gr = [[] for i in range(N + 1)]
D = [] 
for i in range(M):
    u, v, l, a = list(map(int, input().split()))
    D.append([u, v, l, a])
    Gr[v].append(u)
    
dist = bfs(Gr, N)
for u, v, l, a in D:
    if dist[u] == -1 or dist[v] == -1:
        continue
    G[u].append((v, l, a))

dp = [0] * (N + 1)
dp_sum = [0] * (N + 1)
dp[0] = 1
L = topsort(G)
if len(L) != N + 1:
    print("INF")
    exit()

mod = 10**9 + 7    
for u in L:
    for v, l, a in G[u]:
        dp[v] += dp[u] * a
        dp[v] %= mod
        dp_sum[v] += (dp_sum[u] + dp[u] * l) * a
        dp_sum[v] %= mod
        
print(dp_sum[-1])
0