結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー |
![]() |
提出日時 | 2021-08-08 12:43:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 714 ms / 2,500 ms |
コード長 | 2,045 bytes |
コンパイル時間 | 238 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 134,400 KB |
最終ジャッジ日時 | 2024-09-19 08:18:35 |
合計ジャッジ時間 | 16,454 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
from collections import defaultdict, deque, Counterfrom heapq import heappush, heappop, heapifyimport mathimport bisectimport randomfrom itertools import permutations, accumulate, combinations, productimport sysimport stringfrom bisect import bisect_left, bisect_rightfrom math import factorial, ceil, floorfrom operator import mulfrom functools import reduceimport pprintsys.setrecursionlimit(10 ** 9)INF = 10 ** 20def LI(): return list(map(int, sys.stdin.readline().split()))def I(): return int(sys.stdin.readline())def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')def IR(n): return [I() for i in range(n)]def LIR(n): return [LI() for i in range(n)]def SR(n): return [S() for i in range(n)]def LSR(n): return [LS() for i in range(n)]def SRL(n): return [list(S()) for i in range(n)]def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]mod = 10**9+7n,m=LI()G=[[]for _ in range(n+1)]for _ in range(m):u,v,l,a=LI()G[u]+=[(v,l,a)]def topological_sort(G):n = len(G)in_degree = [0] * nfor u in range(n):for v,_,_ in G[u]:in_degree[v] += 1topological_order = []que = deque()for i in range(n):if in_degree[i] == 0:que += [i]while que:u = que.pop()topological_order += [u]for v,_,_ in G[u]:in_degree[v] -= 1if in_degree[v] == 0:que += [v]return topological_orderq=deque([0])D=[0]*(n+1)D2=[0]*(n+1)D2[0]=1D[0]=0order=topological_sort(G)s=set()for ui in order:s.add(ui)for vi,li,ai in G[ui]:D[vi]+=(D[ui]+D2[ui]*li)*ai%modD[vi]%=modD2[vi]+=D2[ui]*aiD2[vi]%=modqq=[0]D3=[0]*(n+1)D3[0]=1while qq:uk=qq.pop()for vk,_,_ in G[uk]:if D3[vk]:continueD3[vk]=1qq+=[vk]if not D2[n] and D3[n]:print("INF")else:print(D[n])