-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumCosttoConnectTwoGroupsofPoints.cpp
More file actions
31 lines (30 loc) · 1.17 KB
/
minimumCosttoConnectTwoGroupsofPoints.cpp
File metadata and controls
31 lines (30 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Source: https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/
// Author: Miao Zhang
// Date: 2021-05-19
/************************************************************
* dp[i][s]: the first group: 0-i, second group: set,1 << (len(cost[0]))
* dp[i][s | (1 << j)] = min(dp[i][s | (1 << j)],
* dp[i][s] + cost[i][j],
* dp[i - 1][s] + cost[i][j])
* dp[0][0] = 0
* dp[m][1 << n - 1]
************************************************************/
class Solution {
public:
int connectTwoGroups(vector<vector<int>>& cost) {
int m = cost.size();
int n = cost[0].size();
vector<vector<int>> dp(m + 1, vector<int>(1 << n, INT_MAX / 2));
dp[0][0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int s = 0; s < 1 << n; s++) {
dp[i + 1][s | (1 << j)] = min({dp[i + 1][s | (1 << j)],
dp[i + 1][s] + cost[i][j],
dp[i][s] + cost[i][j]});
}
}
}
return dp[m].back();
}
};