-
Notifications
You must be signed in to change notification settings - Fork 481
/
1066.cpp
31 lines (29 loc) · 861 Bytes
/
1066.cpp
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
class Solution
{
public:
int assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes)
{
n = workers.size(), m = bikes.size();
int k = 1 << m;
vector<vector<int>> f(n, vector<int>(k, -1));
return dfs(0, 0, workers, bikes, f);
}
private:
int n, m;
int dis(vector<int>& a, vector<int>& b)
{
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
}
int dfs(int i, int j, vector<vector<int>>& workers, vector<vector<int>>& bikes, vector<vector<int>>& f)
{
if(i == n) return 0;
if(~f[i][j]) return f[i][j];
f[i][j] = INT_MAX;
for(int k = 0; k < m; ++k)
{
if(!(j & (1 << k)))
f[i][j] = min(f[i][j], dfs(i+1, j | (1 << k), workers, bikes, f) + dis(workers[i], bikes[k]));
}
return f[i][j];
}
};