程式碼隨想錄演算法訓練營day60| 城市間的貨物運輸之1、2、3

Tristan241001發表於2024-11-28

學習資料:https://www.programmercarl.com/kamacoder/0094.城市間貨物運輸I-SPFA.html

SPFA演算法(Bellman_ford佇列最佳化演算法)

學習記錄
94.城市間的貨物運輸(兩種方法)

點選檢視程式碼
# 法二
import collections

def main():
    n, m = map(int, input().split())
    edges = [[] for _ in range(n+1)]
    for _ in range(m):
        src, dest, weight = map(int, input().strip().split())
        edges[src].append([dest, weight])
    minDist = [float("inf")] * (n+1)
    minDist[1] = 0
    que = collections.deque([1])
    visited = [False]*(n+1)
    visited[1]=True
    
    while que:
        cur = que.popleft()
        visited[cur] = False
        for dest, weight in edges[cur]:
            if minDist[cur] != float("inf") and minDist[cur]+weight<minDist[dest]:
                minDist[dest] = minDist[cur]+weight
                if visited[dest] == False:
                    que.append(dest)
                    visited[dest]=True
    
    if minDist[-1] == float("inf"):
        return "unconnected"
    return minDist[-1]

if __name__ == "__main__":
    print(main())


# def main():
#     n, m = map(int, input().split())
#     edges = []
#     for _ in range(m):
#         src, dest, weight = map(int, input().split())
#         edges.append([src, dest, weight])
#     minDist = [float("inf")]*(n+1)
#     minDist[1]=0
    
#     for i in range(1, n):
#         updated = False
#         for src, dest, weight in edges:
#             if minDist[src] != float("inf") and minDist[src]+weight<minDist[dest]:
#                 minDist[dest] = minDist[src]+weight
#                 updated = True
#         if not updated:
#             break
    
#     if minDist[-1]==float("inf"):
#         return "unconnected"
#     return minDist[-1]

# if __name__ == "__main__":
#     print(main())

95.城市間貨物運輸2(判斷負權迴路)

點選檢視程式碼
import sys

def main():
    input = sys.stdin.read
    data = input().split()
    index = 0
    
    n = int(data[index])
    index += 1
    m = int(data[index])
    index += 1
    
    grid = []
    for i in range(m):
        p1 = int(data[index])
        index += 1
        p2 = int(data[index])
        index += 1
        val = int(data[index])
        index += 1
        # p1 指向 p2,權值為 val
        grid.append([p1, p2, val])

    start = 1  # 起點
    end = n    # 終點

    minDist = [float('inf')] * (n + 1)
    minDist[start] = 0
    flag = False

    for i in range(1, n + 1):  # 這裡我們鬆弛n次,最後一次判斷負權迴路
        for side in grid:
            from_node = side[0]
            to = side[1]
            price = side[2]
            if i < n:
                if minDist[from_node] != float('inf') and minDist[to] > minDist[from_node] + price:
                    minDist[to] = minDist[from_node] + price
            else:  # 多加一次鬆弛判斷負權迴路
                if minDist[from_node] != float('inf') and minDist[to] > minDist[from_node] + price:
                    flag = True

    if flag:
        print("circle")
    elif minDist[end] == float('inf'):
        print("unconnected")
    else:
        print(minDist[end])

if __name__ == "__main__":
    main()

96.城市間貨物運輸3(單源有限最短路)
沒寫!竟然沒有Python解法,下次學習一下演算法思路算咯

PS:今天真真是抄的,明天來仔細學吧,今天要累死了。再一次堅定了不要996。吃的也不好吃炒飯膩的很,
今天天氣真好,銀杏樹黃咯,好美啊
工作和論文進展+1

相關文章