結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー roarisroaris
提出日時 2021-01-23 20:53:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,770 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 86,856 KB
実行使用メモリ 136,232 KB
最終ジャッジ日時 2023-08-30 05:16:26
合計ジャッジ時間 22,680 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,788 KB
testcase_01 AC 99 ms
71,704 KB
testcase_02 AC 97 ms
71,680 KB
testcase_03 AC 99 ms
71,756 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 134 ms
78,076 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 140 ms
78,016 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 234 ms
105,900 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 205 ms
93,768 KB
testcase_24 AC 147 ms
84,156 KB
testcase_25 AC 339 ms
103,036 KB
testcase_26 AC 463 ms
116,912 KB
testcase_27 AC 310 ms
103,836 KB
testcase_28 AC 270 ms
96,696 KB
testcase_29 AC 294 ms
103,820 KB
testcase_30 AC 268 ms
96,820 KB
testcase_31 AC 229 ms
96,772 KB
testcase_32 AC 255 ms
102,144 KB
testcase_33 AC 427 ms
112,244 KB
testcase_34 AC 451 ms
115,812 KB
testcase_35 AC 455 ms
124,100 KB
testcase_36 AC 401 ms
117,420 KB
testcase_37 AC 233 ms
101,196 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+100)
from collections import *

def bfs():
    dist = [-1]*(N+1)
    dist[0] = 0
    q = deque([0])
    
    while q:
        v = q.popleft()
        
        for nv in g[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist

def rbfs():
    dist = [-1]*(N+1)
    dist[N] = 0
    q = deque([N])
    
    while q:
        v = q.popleft()
        
        for nv in rg[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist

def dfs(v, pv):
    global cycle
    
    seen[v] = True
    
    for nv in g[v]:
        if dist1[nv]==-1 or dist2[nv]==-1:
            continue
        
        if finished[nv]:
            continue
        
        if seen[nv] and not finished[nv]:
            cycle = True
            return

        dfs(nv, v);
        
        if cycle:
            return
    
    finished[v] = True

N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
g = [[] for _ in range(N+1)]
rg = [[] for _ in range(N+1)]

for _ in range(M):
    u, v, l, a = map(int, input().split())
    G[u].append((v, l, a))
    g[u].append(v)
    rg[v].append(u)

dist1, dist2 = bfs(), rbfs()
seen = [False]*(N+1)
finished = [False]*(N+1)
cycle = False
dfs(0, -1)

if cycle:
    print('INF')
    exit()

dpl = [0]*(N+1)
dpc = [0]*(N+1)
dpc[0] = 1
MOD = 10**9+7
q = deque([0])

while q:
    nq = deque([])
    
    while q:
        v = q.popleft()
        
        for nv, l, a in G[v]:
            dpl[nv] += a*dpl[v]+a*dpc[v]*l
            dpl[nv] %= MOD
            dpc[nv] += dpc[v]*a
            dpc[nv] %= MOD
            nq.append(nv)
    
    q = nq

print(dpl[N])
0