其他分享
首页 > 其他分享> > leetcode787+s到目的地最多停K次最小花费,Dp

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];
    }
};

标签:src,int,within,最多停,flights,vector,dp,leetcode787,Dp
来源: https://blog.csdn.net/u013554860/article/details/87908574