-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1473_minCost.cc
339 lines (325 loc) · 7.91 KB
/
Problem_1473_minCost.cc
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <cstdint>
#include <vector>
using namespace std;
class Solution
{
public:
static constexpr int NA = INT32_MAX;
static constexpr int MAXN = 101;
static constexpr int MAXT = 101;
static constexpr int MAXC = 21;
int house[MAXN];
int cost[MAXN][MAXC];
int n, t, c;
// 原始测试页面的数据描述非常绕,一律转化成课上描述的形式
// 房子编号从1开始,颜色编号从1开始,颜色0代表没有涂色
// build方法就是转化逻辑
void build(vector<int>& houses, vector<vector<int>>& costs, int hsize, int csize, int tsize)
{
n = hsize;
t = tsize;
c = csize;
for (int i = 1; i <= n; i++)
{
house[i] = houses[i - 1];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= c; j++)
{
cost[i][j] = costs[i - 1][j - 1];
}
}
}
// 递归 + 记忆化搜索,不优化枚举
// 时间复杂度O(n * t * c平方)
int minCost1(vector<int>& houses, vector<vector<int>>& costs, int hsize, int csize, int tsize)
{
build(houses, costs, hsize, csize, tsize);
t++;
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(t + 1, vector<int>(c + 1)));
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= t; j++)
{
for (int v = 0; v <= c; v++)
{
dp[i][j][v] = -1;
}
}
}
int ans = f1(n, t, 0, dp);
return ans == NA ? -1 : ans;
}
// 1...i范围的房子去确定颜色,i+1号房子的颜色已经涂成了v
// 1...i+1范围的房子必须凑齐j个街区,返回最少的花费
// 如果做不到返回NA
int f1(int i, int j, int v, vector<vector<vector<int>>>& dp)
{
if (j == 0)
{
return NA;
}
if (i == 0)
{
return j == 1 ? 0 : NA;
}
if (dp[i][j][v] != -1)
{
return dp[i][j][v];
}
int ans = NA;
if (house[i] != 0)
{
if (house[i] == v)
{
ans = f1(i - 1, j, house[i], dp);
}
else
{
ans = f1(i - 1, j - 1, house[i], dp);
}
}
else
{
for (int color = 1, next; color <= c; color++)
{
if (color == v)
{
next = f1(i - 1, j, color, dp);
}
else
{
next = f1(i - 1, j - 1, color, dp);
}
if (next != NA)
{
ans = std::min(ans, next + cost[i][color]);
}
}
}
dp[i][j][v] = ans;
return ans;
}
// 严格位置依赖的动态规划,不优化枚举
// 时间复杂度O(n * t * c平方)
int minCost2(vector<int>& houses, vector<vector<int>>& costs, int hsize, int csize, int tsize)
{
build(houses, costs, hsize, csize, tsize);
t++;
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(t + 1, vector<int>(c + 1)));
for (int i = 0; i <= n; i++)
{
for (int v = 0; v <= c; v++)
{
dp[i][0][v] = NA;
}
}
for (int j = 1; j <= t; j++)
{
for (int v = 0; v <= c; v++)
{
dp[0][j][v] = j == 1 ? 0 : NA;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= t; j++)
{
for (int v = 0; v <= c; v++)
{
int ans = NA;
if (house[i] != 0)
{
if (house[i] == v)
{
ans = dp[i - 1][j][house[i]];
}
else
{
ans = dp[i - 1][j - 1][house[i]];
}
}
else
{
for (int color = 1, next; color <= c; color++)
{
if (color == v)
{
next = dp[i - 1][j][color];
}
else
{
next = dp[i - 1][j - 1][color];
}
if (next != NA)
{
ans = std::min(ans, next + cost[i][color]);
}
}
}
dp[i][j][v] = ans;
}
}
}
int ans = dp[n][t][0];
return ans == NA ? -1 : ans;
}
// 空间压缩版本,不优化枚举
// 时间复杂度O(n * t * c平方)
int minCost3(vector<int>& houses, vector<vector<int>>& costs, int hsize, int csize, int tsize)
{
build(houses, costs, hsize, csize, tsize);
t++;
vector<vector<int>> memo(t + 1, vector<int>(c + 1));
vector<vector<int>> dp(t + 1, vector<int>(c + 1));
// 因为此时只有memo、dp两份空间
// 所以让memo[0][v] = dp[0][v] = NA
// 这样一来,在滚动更新时,不管i是多少,只要j==0,那么结果都是NA
for (int v = 0; v <= c; v++)
{
memo[0][v] = dp[0][v] = NA;
}
// i == 0时,结果填入memo表
for (int j = 1; j <= t; j++)
{
for (int v = 0; v <= c; v++)
{
memo[j][v] = j == 1 ? 0 : NA;
}
}
// 滚动更新
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= t; j++)
{
for (int v = 0; v <= c; v++)
{
int ans = NA;
if (house[i] != 0)
{
if (house[i] == v)
{
ans = memo[j][house[i]];
}
else
{
ans = memo[j - 1][house[i]];
}
}
else
{
for (int color = 1, next; color <= c; color++)
{
if (color == v)
{
next = memo[j][color];
}
else
{
next = memo[j - 1][color];
}
if (next != NA)
{
ans = std::min(ans, next + cost[i][color]);
}
}
}
dp[j][v] = ans;
}
}
auto tmp = memo;
memo = dp;
dp = tmp;
}
int ans = memo[t][0];
return ans == NA ? -1 : ans;
}
// 最优解
// 优化枚举 + 空间压缩
// 时间复杂度O(n * t * c)
int minCost4(vector<int>& houses, vector<vector<int>>& costs, int hsize, int csize, int tsize)
{
build(houses, costs, hsize, csize, tsize);
t++;
vector<vector<int>> memo(t + 1, vector<int>(c + 1));
vector<vector<int>> dp(t + 1, vector<int>(c + 1));
for (int v = 0; v <= c; v++)
{
memo[0][v] = dp[0][v] = NA;
}
for (int j = 1; j <= t; j++)
{
for (int v = 0; v <= c; v++)
{
memo[j][v] = j == 1 ? 0 : NA;
}
}
// pre[s] : dp[i-1][j][1...s] + cost[i][1...s]中的最小值
// suf[s] : dp[i-1][j][s...c] + cost[i][s...c]中的最小值
vector<int> pre(c + 2);
vector<int> suf(c + 2);
pre[0] = suf[c + 1] = NA;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= t; j++)
{
// 预处理结构优化前缀枚举
for (int v = 1; v <= c; v++)
{
pre[v] = pre[v - 1];
if (memo[j - 1][v] != NA)
{
pre[v] = std::min(pre[v], memo[j - 1][v] + cost[i][v]);
}
}
// 预处理结构优化后缀枚举
for (int v = c; v >= 1; v--)
{
suf[v] = suf[v + 1];
if (memo[j - 1][v] != NA)
{
suf[v] = std::min(suf[v], memo[j - 1][v] + cost[i][v]);
}
}
// 填写dp表
for (int v = 0; v <= c; v++)
{
int ans = NA;
if (house[i] != 0)
{
if (house[i] == v)
{
ans = memo[j][house[i]];
}
else
{
ans = memo[j - 1][house[i]];
}
}
else
{
if (v == 0)
{
ans = suf[1];
}
else
{
ans = std::min(pre[v - 1], suf[v + 1]);
if (memo[j][v] != NA)
{
ans = std::min(ans, memo[j][v] + cost[i][v]);
}
}
}
dp[j][v] = ans;
}
}
auto tmp = memo;
memo = dp;
dp = tmp;
}
int ans = memo[t][0];
return ans == NA ? -1 : ans;
}
};