class Solution { public: /** * * @param breadNum int整型 * @param beverageNum int整型 * @param packageSum int整型vector<vector<>> 每个一维数组中有三个数,依次表示这个包装里面的面包数量、饮料数量、花费 * @return int整型 */ int minCost(int breadNum, int beverageNum, vector<vector<int> >& packageSum) { // write code here const int N = 2e3 + 7; int dp[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = 2e9 + 7; } } dp[0][0] = 0; int n = packageSum.size(); for (int i = 0; i < n; i++) { int x = packageSum[i][0]; int y = packageSum[i][1]; int t = packageSum[i][2]; for (int j = breadNum; j >= 0; j--) { for (int k = beverageNum; k >= 0; k--) { dp[min(j + x, breadNum)][min(k + y, beverageNum)] = min(dp[min(j + x, breadNum)][min(k + y, beverageNum)], dp[j][k] + t); } } } return dp[breadNum][beverageNum]; } };
原文:https://www.cnblogs.com/HighLights/p/13276595.html