leetcode.K站中转(python)

开始准备用dfs深度搜索,发现n=100,dfs可能会超时,即使用了剪枝。

class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        length = k + 2
        ans = float('inf')
        rec = []
        vis = [True]*n
        edge = defaultdict(list)
        for f, t, p in flights:
            edge[f].append([t, p])

        def dfs(node, spend):
            nonlocal ans
            rec.append(node)
            vis[node] = False
            if node == dst:
                ans = min(ans, spend)
            elif len(rec) < length:
                for nex, p in edge[node]:
                    if not vis[nex]: continue
                    dfs(nex, spend + p)
            rec.pop()
            vis[node] = True
        dfs(src, 0)
        return ans if ans != float('inf') else -1

理所当然的想用bfs,n=100肯定不会超时,谁知道题目针对,这次内存超了。因为题目中 

  • 0 <= flights.length <= (n * (n - 1) / 2)

相当于100*99/2,大概5000条路线呗。这就超了??? 

class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        edge = defaultdict(dict)
        for f, t, p in flights:
            edge[f][t] = p
        qu = deque()
        ans = float('inf')
        qu.append([src, 0, -1])
        while qu:
            node, spend, num = qu.popleft()
            if num > k:continue
            if node == dst:
                ans = min( ans, spend )
                continue
            for nex in edge[node]:
                qu.append([nex, spend + edge[node][nex], num + 1])
        return ans if ans != float('inf') else -1

看见大佬的优化过程,叹为观止。

使用最小堆,每次弹出列表中最小花费的路径,利用steps避免走成一个环。发现我之前的问题,应该就是走进一个环中,导致数据增多,内存超了。

import heapq as pq
class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        edge = defaultdict(dict)
        for f, t, p in flights:
            edge[f][t] = p
        qu = [[0, src, -1]]
        pq.heapify(qu)
        steps = [k+1]*n
        while qu:
            spend, node, num = pq.heappop(qu)
            if steps[node] <= num:continue
            steps[node] = num
            if node == dst:
                return spend
            for nex in edge[node]:
                pq.heappush(qu, [spend + edge[node][nex], nex, num + 1])
        return -1

下面是官方题解,使用dp。

使用dp动态规划算法,设dp【t】【i】,表示转到第t站,从src到达i所需的最小花费数;

那么dp【t】【i】 = min(dp【t】【i】,dp【t-1】【j】+cost【j】【i】),遍历所有路线。

class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        dp = [[float('inf')]*(n) for _ in range(k+2)]
        dp[0][src] = 0
        ans = float('inf')
        for t in range(1, k+2):
            for j, i, p in flights:
                dp[t][i] = min(dp[t][i], dp[t-1][j] + p)
                if i == dst: ans = min (ans, dp[t][i])
        return ans if ans != float('inf') else -1

 这个动态规划,内核也就是bfs。第一次只更新了从src出发到达的节点。这个方法稍稍不如bfs,因为每一步都走了一些不能走的点。

相关推荐

  1. 一个Python

    2024-05-15 21:18:10       54 阅读
  2. 阿里巴巴中国获得1688商品详情 API

    2024-05-15 21:18:10       52 阅读
  3. 阿里巴巴中国获得1688商品详情 API

    2024-05-15 21:18:10       65 阅读
  4. 1688中国获得工厂档案信息 API

    2024-05-15 21:18:10       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-15 21:18:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-15 21:18:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-15 21:18:10       82 阅读
  4. Python语言-面向对象

    2024-05-15 21:18:10       91 阅读

热门阅读

  1. yolo进行视频检测结果没有生成

    2024-05-15 21:18:10       30 阅读
  2. Linux函数

    2024-05-15 21:18:10       29 阅读
  3. nvr国标sip端口信息异常的处理

    2024-05-15 21:18:10       33 阅读
  4. SpringBoot+Mock Mvc测试web接口增删改查、导入导出

    2024-05-15 21:18:10       31 阅读
  5. 微信小程序更新日志

    2024-05-15 21:18:10       32 阅读
  6. 设计模式之——单例模式

    2024-05-15 21:18:10       34 阅读
  7. android设计模式-单例模式

    2024-05-15 21:18:10       38 阅读
  8. 【设计模式】单例模式-学习记录

    2024-05-15 21:18:10       34 阅读
  9. redis中的大key问题

    2024-05-15 21:18:10       28 阅读
  10. Android Studio实现简易音乐播放器(期末作业)

    2024-05-15 21:18:10       33 阅读
  11. Android security知识点总结

    2024-05-15 21:18:10       26 阅读