結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー rlangevinrlangevin
提出日時 2024-01-08 01:37:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 548 ms / 2,500 ms
コード長 1,589 bytes
コンパイル時間 2,438 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 149,196 KB
最終ジャッジ日時 2024-01-08 01:38:19
合計ジャッジ時間 16,173 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,604 KB
testcase_01 AC 41 ms
55,604 KB
testcase_02 AC 41 ms
55,604 KB
testcase_03 AC 41 ms
55,604 KB
testcase_04 AC 42 ms
55,604 KB
testcase_05 AC 41 ms
55,604 KB
testcase_06 AC 43 ms
55,604 KB
testcase_07 AC 42 ms
55,604 KB
testcase_08 AC 93 ms
78,584 KB
testcase_09 AC 86 ms
77,400 KB
testcase_10 AC 104 ms
78,328 KB
testcase_11 AC 94 ms
78,328 KB
testcase_12 AC 121 ms
79,616 KB
testcase_13 AC 353 ms
119,728 KB
testcase_14 AC 476 ms
125,868 KB
testcase_15 AC 442 ms
126,452 KB
testcase_16 AC 341 ms
110,328 KB
testcase_17 AC 216 ms
104,304 KB
testcase_18 AC 522 ms
140,756 KB
testcase_19 AC 492 ms
140,724 KB
testcase_20 AC 513 ms
140,808 KB
testcase_21 AC 499 ms
140,704 KB
testcase_22 AC 502 ms
140,656 KB
testcase_23 AC 187 ms
92,980 KB
testcase_24 AC 141 ms
94,036 KB
testcase_25 AC 357 ms
113,644 KB
testcase_26 AC 494 ms
122,308 KB
testcase_27 AC 370 ms
106,864 KB
testcase_28 AC 292 ms
106,172 KB
testcase_29 AC 341 ms
114,624 KB
testcase_30 AC 295 ms
102,696 KB
testcase_31 AC 222 ms
96,856 KB
testcase_32 AC 254 ms
102,480 KB
testcase_33 AC 509 ms
126,660 KB
testcase_34 AC 548 ms
131,376 KB
testcase_35 AC 400 ms
129,252 KB
testcase_36 AC 354 ms
125,776 KB
testcase_37 AC 263 ms
119,624 KB
testcase_38 AC 358 ms
149,196 KB
testcase_39 AC 355 ms
149,056 KB
testcase_40 AC 355 ms
149,064 KB
testcase_41 AC 380 ms
149,064 KB
testcase_42 AC 356 ms
149,184 KB
testcase_43 AC 232 ms
123,740 KB
testcase_44 AC 196 ms
118,672 KB
testcase_45 AC 246 ms
118,012 KB
testcase_46 AC 69 ms
87,652 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)]
G1 = [[] for i in range(N + 1)]
G2 = [[] 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])
    G1[u].append(v)
    G2[v].append(u)
   
dist1 = bfs(G1, 0) 
dist2 = bfs(G2, N)
for u, v, l, a in D:
    if dist1[u] == -1 or dist1[v] == -1 or dist2[u] == -1 or dist2[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