【代码随想录算法训练营——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])
相关推荐
AvocadoZeroOneOI3 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz38995 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
booboosui7 小时前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
橘子139 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘20 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
雨白1 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David1 天前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Pniubi1 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
AIgorithmGEEK1 天前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set