封尘韵 edited 3 周,6 天前

请问为什么通不过第三个例子啊,是数太大超过数组最大范围了吗?

import sys
import heapq
input=sys.stdin.read
datas=input().strip().split("\n")

n=int(datas[0][0])
if n > 600:
    print("Error: n must be less than or equal to 600.")
    sys.exit(1)  # 非0状态码表示错误

datas=[i.split(" ") for i in datas[1:]]
edges=[]
for data in datas:
    temp=[int(i) for i in data]
    edges.append(temp)

def get_min_path(edges,n):
    grid=[[] for _ in range(n)]
    for u,v,w in edges:
        grid[u-1].append([v-1,w])
    
    dis=[10**6]*n
    dis[0]=0
    h=[[0,0]]
    while(h):
        cost,x=heapq.heappop(h)
        if cost>dis[x]:
            continue
        for y,w in grid[x]:
            new_w=dis[x]+w
            if new_w<dis[y]:
                dis[y]=new_w
                heapq.heappush(h,[dis[y],y])
    
    if dis[n-1]==10**6:
        return -1
    return dis[n-1]

ans=get_min_path(edges,n)
print(ans)
            
            
    

Comments