代码随想录算法训练营第59天|Bellman_ford 队列优化、bellman_ford判断负权回路、bellman_ford单源有限最短路

Bellman_ford 队列优化

卡码网 94. 城市间货物运输 I
代码随想录

from collections import deque
 
V, E = map(int, input().split())
  
  
graph = [[] for _ in range(V+1)]
 
for i in range(E):
    s,t,v = map(int, input().split())
    graph[s].append([t,v])
 
 
distance = [float("+inf")] * (V+1)
distance[1] = 0
 
que = deque()
que.append(1)
visited = [False] * (V+1)
visited[1] = True
 
while que:
    node = que.popleft()
    visited[node] = False
    for t,v in graph[node]:
        if distance[node] != float("+inf") and distance[node] + v < distance[t]:
            distance[t] = distance[node] + v
            if not visited[t]:
                que.append(t)
                visited[t] = True
    # print(distance)
    # print(visited)
 
print(distance[-1]) if distance[-1] != float("+inf") else print("unconnected")

bellman_ford之判断负权回路

卡码网 95. 城市间货物运输 II
代码随想录

# 超时还有花在io上的时间
import sys
 
input = sys.stdin.read
data = input().split()
index = 0
 
V = int(data[index])
index += 1
E = int(data[index])
index += 1
 
graph = []
for i in range(E):
    p1 = int(data[index])
    index += 1
    p2 = int(data[index])
    index += 1
    val = int(data[index])
    index += 1
    # p1 指向 p2,权值为 val
    graph.append([p1, p2, val])
 
 
distance = [float("+inf")] * (V+1)
distance[1] = 0
for i in range(1, V):
    # 如果不在更新就要退出了
    # updated = False
    for s,t,v in graph:
        if distance[s] != float("+inf") and distance[s] + v < distance[t]:
            distance[t] = distance[s] + v
            # updated = True
    # if not updated:
        # break
        
# 这个算法还可以检测负环,只要再进行一次就好了。
updated = False
for s,t,v in graph:
    if distance[s] != float("+inf") and distance[s] + v < distance[t]:
        distance[t] = distance[s] + v
        updated = True
if updated:
    print("circle")
    exit()
   
print(distance[-1]) if distance[-1] != float("+inf") else print("unconnected")

bellman_ford之单源有限最短路

卡码网 96. 城市间货物运输 III
代码随想录

import sys
 
input = sys.stdin.read
data = input().split()
index = 0
 
V = int(data[index])
index += 1
E = int(data[index])
index += 1
 
graph = []
for i in range(E):
    p1 = int(data[index])
    index += 1
    p2 = int(data[index])
    index += 1
    val = int(data[index])
    index += 1
    # p1 指向 p2,权值为 val
    graph.append([p1, p2, val])

src = int(data[index])
index += 1

dst = int(data[index])
index += 1

k = int(data[index])
index += 1

distance = [float("+inf")] * (V+1)
distance[src] = 0
distance_copy = []
for i in range(0, k+1):
    # 如果不在更新就要退出了
    distance_copy = distance[:]
    for s,t,v in graph:
        if distance[s] != float("+inf") and distance_copy[s] + v < distance[t]:
            distance[t] = distance_copy[s] + v
        
   
print(distance[dst]) if distance[dst] != float("+inf") else print("unreachable")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值