結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 👑 KazunKazun
提出日時 2021-01-20 04:51:08
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 5,813 bytes
コンパイル時間 687 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 230,572 KB
最終ジャッジ日時 2023-08-26 16:25:29
合計ジャッジ時間 37,678 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
72,340 KB
testcase_01 AC 92 ms
72,420 KB
testcase_02 AC 99 ms
71,732 KB
testcase_03 AC 92 ms
72,088 KB
testcase_04 AC 94 ms
71,972 KB
testcase_05 AC 91 ms
72,392 KB
testcase_06 AC 92 ms
72,404 KB
testcase_07 AC 94 ms
72,376 KB
testcase_08 AC 214 ms
89,016 KB
testcase_09 AC 166 ms
80,112 KB
testcase_10 AC 201 ms
84,200 KB
testcase_11 AC 205 ms
86,964 KB
testcase_12 AC 221 ms
84,528 KB
testcase_13 AC 977 ms
186,284 KB
testcase_14 AC 1,078 ms
151,804 KB
testcase_15 AC 1,109 ms
182,224 KB
testcase_16 AC 776 ms
140,964 KB
testcase_17 AC 569 ms
163,904 KB
testcase_18 AC 1,372 ms
223,108 KB
testcase_19 AC 1,559 ms
223,372 KB
testcase_20 AC 1,297 ms
223,108 KB
testcase_21 AC 1,280 ms
222,820 KB
testcase_22 AC 1,299 ms
225,224 KB
testcase_23 AC 434 ms
127,332 KB
testcase_24 AC 159 ms
78,440 KB
testcase_25 AC 666 ms
137,632 KB
testcase_26 AC 895 ms
148,604 KB
testcase_27 AC 596 ms
118,196 KB
testcase_28 AC 540 ms
124,512 KB
testcase_29 AC 544 ms
118,620 KB
testcase_30 AC 535 ms
124,564 KB
testcase_31 AC 475 ms
130,340 KB
testcase_32 AC 424 ms
116,604 KB
testcase_33 AC 841 ms
135,804 KB
testcase_34 AC 1,027 ms
169,692 KB
testcase_35 AC 1,199 ms
204,560 KB
testcase_36 AC 1,200 ms
193,556 KB
testcase_37 AC 352 ms
106,088 KB
testcase_38 AC 325 ms
84,160 KB
testcase_39 AC 325 ms
84,248 KB
testcase_40 AC 325 ms
83,852 KB
testcase_41 AC 323 ms
83,792 KB
testcase_42 AC 345 ms
84,176 KB
testcase_43 AC 503 ms
230,572 KB
testcase_44 TLE -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Digraph:
    """重み[なし]有向グラフを生成する.

    """

    #入力定義
    def __init__(self,vertex=[]):
        self.vertex=set(vertex)

        self.edge_number=0
        self.vertex_number=len(vertex)

        self.adjacent_out={v:set() for v in vertex} #出近傍(vが始点)
        self.adjacent_in={v:set() for v in vertex} #入近傍(vが終点)

    #頂点の追加
    def add_vertex(self,*adder):
        for v in adder:
            if v not in self.vertex:
                self.adjacent_in[v]=set()
                self.adjacent_out[v]=set()

                self.vertex_number+=1
                self.vertex.add(v)

    #辺の追加
    def add_edge(self,From,To):
        for v in [From,To]:
            if v not in self.vertex:
                self.add_vertex(v)

        if To not in self.adjacent_in[From]:
            self.edge_number+=1

        self.adjacent_out[From].add(To)
        self.adjacent_in[To].add(From)

    #辺を除く
    def remove_edge(self,From,To):
        for v in [From,To]:
            if v not in self.vertex:
                self.add_vertex(v)

        if To in self.adjacent_out[From]:
            self.adjacent_out[From].remove(To)
            self.adjacent_in[To].remove(From)
            self.edge_number-=1

    #頂点を除く
    def remove_vertex(self,*vertexes):
        for  v in vertexes:
            if v in self.vertex:
                self.vertex_number-=1

                for u in self.adjacent_out[v]:
                    self.adjacent_in[u].remove(v)
                    self.edge_number-=1
                del self.adjacent_out[v]

                for u in self.adjacent_in[v]:
                    self.adjacent_out[u].remove(v)
                    self.edge_number-=1
                del self.adjacent_in[v]

    #Walkの追加
    def add_walk(self,*walk):
        N=len(walk)
        for k in range(N-1):
            self.add_edge(walk[k],walk[k+1])

    #Cycleの追加
    def add_cycle(self,*cycle):
        self.add_walk(*cycle)
        self.add_edge(cycle[-1],cycle[0])

    #頂点の交換
    def __vertex_swap(self,p,q):
        self.vertex.sort()

    #グラフに頂点が存在するか否か
    def vertex_exist(self,v):
        return v in self.vertex

    #グラフに辺が存在するか否か
    def edge_exist(self,From,To):
        if not(self.vertex_exist(From) and self.vertex_exist(To)):
            return False
        return To in self.adjacent_out[From]

    #近傍
    def neighbohood(self,v):
        if not self.vertex_exist(v):
            return []
        return list(self.adjacent[v])

    #出次数
    def out_degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent_out[v])

    #入次数
    def in_degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent_in[v])

    #次数
    def degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return self.out_degree(v)-self.in_degree(v)

    #頂点数
    def vertex_count(self):
        return len(self.vertex)

    #辺数
    def edge_count(self):
        return self.edge_number

    #頂点vを含む連結成分
    def connected_component(self,v):
        pass

#強連結成分に分解
def Strongly_Connected_Component_Decomposition(D,Mode=0):
    """有向グラフDを強連結成分に分解

    Mode:
    0(Defalt)---各強連結成分の頂点のリスト
    1        ---各頂点が属している強連結成分の番号
    2        ---0,1の両方

    ※0で帰ってくるリストは各強連結成分に関してトポロジカルソートである.
    """
    Group={v:0 for v in D.adjacent_out}
    Order=[]

    for v in D.adjacent_out:
        if Group[v]:continue

        S=[v]
        Group[v]=-1

        while S:
            u=S.pop()
            for w in D.adjacent_out[u]:
                if Group[w]:continue
                Group[w]=-1
                S.append(u)
                S.append(w)
                break
            else:
                Order.append(u)

    k=0
    for v in Order[::-1]:
        if Group[v]!=-1:
            continue

        S=[v]
        Group[v]=k

        while S:
            u=S.pop()
            for w in D.adjacent_in[u]:
                if Group[w]!=-1:
                    continue

                Group[w]=k
                S.append(w)
        k+=1

    if Mode==0 or Mode==2:
        T=[[] for _ in range(k)]
        for v in D.adjacent_out:
            T[Group[v]].append(v)

    if Mode==0:
        return T
    elif Mode==1:
        return Group
    else:
        return (Group,T)
#================================================
def f(p,z):
    a,w=p
    x,y=z

    return [(x*a)%Mod,a*y+x*w]
#================================================
import sys
from collections import defaultdict

input=sys.stdin.readline

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

D=Digraph(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())
    D.add_edge(u,v)

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

G,T=Strongly_Connected_Component_Decomposition(D,2)

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 T:
    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