5,6号测试点过不了,求助
查看原帖
5,6号测试点过不了,求助
542068
江小白芜湖楼主2021/9/19 18:24
from copy import deepcopy

x=[int(i) for i in input().split(' ')]

n=x[0]

m=x[1]

#我到哪

go={}

#我从哪来

fro={}

#我有几个爹

fath={}

#权

ne={}

#我的最长路径

lu={}

for i in range(n):

    go[i+1]=[]

    fro[i+1]=[]

    fath[i+1]=0

    ne[i+1]={}

    lu[i+1]=0

for i in range(m):

    b=[int(o) for o in input().split(' ')]

    u=b[0]

    v=b[1]

    w=b[2]

    go[u].append(v)

    fro[v].append(u)

    fath[v]+=1

    if v in ne[u]:

        ff=ne[u][v]

    else:

        ne[u][v]=0

        ff=0

    if ff<w:

        ne[u][v]=w

queue=[]

for i in fro:

    if fath[i]==0:

        fath[i]='n'

        queue.append(i)

visited=[]

while queue:

    now=queue.pop(0)

    for i in go[now]:

        fath[i]-=1

    for i in fath:

        if fath[i]==0:

            if i not in visited:    

                visited.append(i)

                queue.append(i)

    zhi=lu[now]

    for i in ne[now]:

        le=lu[i]

        xle=zhi

        g=ne[now][i]

        xle+=g

        if xle>le:

            lu[i]=xle

if n not in visited:

    print(-1)

else:

    print(lu[n])   
2021/9/19 18:24
加载中...