【代码随想录算法训练营——Day60】图论——94.城市间货物运输I、95.城市间货物运输II、96.城市间货物运输III

卡码网题目链接

https://kamacoder.com/problempage.php?pid=1152

https://kamacoder.com/problempage.php?pid=1153

https://kamacoder.com/problempage.php?pid=1154

题解
94.城市间货物运输I

和昨天那题思路类似,加了个队列优化,仿照着c++代码写的。

95.城市间货物运输II

具体看题解,代码也是仿照题解写的。

96.城市间货物运输III

看题解的感悟:理解思路挺重要,还有了4个拓展,联系了前几天的算法。

代码部分:要加updated更新记录,防止时间超限,题解也是这么写的

代码

python 复制代码
#94.城市间货物运输I
from collections import deque
class Edge():
    def __init__(self, to, val):
        self.to = to
        self.val = val

if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = [[] for _ in range(n + 1)]
    isInQueue = [False] * (n + 1)
    for _ in range(m):
        s, t, v = map(int, input().split())
        graph[s].append(Edge(t, v))
    start = 1
    end = n
    minDist = [float('inf')] * (n + 1)
    minDist[start] = 0
    queue = deque()
    queue.append(start)
    isInQueue[start] = True
    while queue:
        node = queue.popleft()
        isInQueue[node] = False
        for edge in graph[node]:
            fromm, to, val = node, edge.to, edge.val
            if minDist[to] > minDist[fromm] + val:
                minDist[to] = minDist[fromm] + val
                if isInQueue[to] == False:
                    queue.append(to)
                    isInQueue[to] = True
    if minDist[end] == float('inf'):
        print("unconnected")
    else:
        print(minDist[end])
python 复制代码
#95.城市间货物运输II
if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = []
    for _ in range(m):
        graph.append(list(map(int, input().split())))
    minDist = [float('inf')] * (n + 1)
    start, end = 1, n
    minDist[start] = 0
    flag = False
    for i in range(1, n + 1): # 多循环一次
        for side in graph:
            fromm, to, val = side[0], side[1], side[2]
            if i < n:
                if minDist[fromm] != float('inf') and minDist[to] > minDist[fromm] + val:
                    minDist[to] = minDist[fromm] + val
            else:
                if minDist[fromm] != float('inf') and minDist[to] > minDist[fromm] + val:
                    flag = True
    if flag:
        print("circle")
    elif minDist[end] == float('inf'):
        print("unconnected")
    else:
        print(minDist[end])
python 复制代码
#96.城市间货物运输III
if __name__ == "__main__":
    n, m = map(int, input().split())
    graph = []
    for _ in range(m):
        graph.append(list(map(int, input().split())))
    src, dst, k = map(int, input().split())
    minDist = [float('inf')] * (n + 1)
    minDist[src] = 0
    for i in range(k + 1):
        updated = False
        minDist_copy = minDist.copy()
        for side in graph:
            fromm, to, val = side[0], side[1], side[2]
            if minDist_copy[fromm] != float('inf') and minDist[to] > minDist_copy[fromm] + val:
                minDist[to] = minDist_copy[fromm] + val
                updated = True
        if not updated:
            break
    if minDist[dst] == float('inf'):
        print("unreachable")
    else:
        print(minDist[dst])
相关推荐
前端摸鱼匠1 天前
【AI大模型春招面试题11】什么是模型的“涌现能力”(Emergent Ability)?出现条件是什么?
人工智能·算法·ai·自然语言处理·面试·职场和发展
MORE_771 天前
leecode-合并区间-贪心算法
算法·贪心算法
2401_873204651 天前
分布式系统安全通信
开发语言·c++·算法
sw1213891 天前
C++中的代理模式实战
开发语言·c++·算法
ballball~~1 天前
ISP-CCM(Color Correction Matrix)
图像处理·数码相机·算法
Sunshine for you1 天前
实时操作系统中的C++
开发语言·c++·算法
gregmankiw1 天前
Nemotron架构(Mamba3+Transformer+Moe)
android·深度学习·transformer
中科院提名者1 天前
BPE 算法的硬核拆解——理解词表(Vocabulary)是如何从零训练出来的,以及字符串是如何被切碎的
算法
「QT(C++)开发工程师」1 天前
C++11三大核心特性深度解析:类型特征、时间库与原子操作
java·c++·算法
乐分启航1 天前
SliMamba:十余K参数量刷新SOTA!高光谱分类的“降维打击“来了
java·人工智能·深度学习·算法·机器学习·分类·数据挖掘