leetcode787+s到目的地最多停K次最小花费,Dp

https://leetcode.com/problems/cheapest-flights-within-k-stops/

class Solution {
public:
    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
        vector<vector<int>> dp(K+2, vector<int>(n, 1e9));
        dp[0][src] = 0;
        for(int i=1; i<=K+1; i++){
            dp[i][src] = 0;
            for(auto flight: flights){
                dp[i][flight[1]] = min(dp[i][flight[1]], dp[i-1][flight[0]]+flight[2]);
            }
        }
        return dp[K+1][dst] == 1e9?-1:dp[K+1][dst];
    }
};

leetcode787+s到目的地最多停K次最小花费,Dp

上一篇:nextjs代理转发fetch请求封装


下一篇:使用collect将R连接到MySQL时连接丢失