/*
Zadanie: Raisins
Autor: Tomasz Kwiatkowski
*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
const int MAXN = 57;
int pref[MAXN][MAXN];
int dp[MAXN][MAXN][MAXN][MAXN];
int sum(int x1, int y1, int x2, int y2)
{
return pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int x1 = 1; x1 <= n; ++x1)
for (int y1 = 1; y1 <= m; ++y1)
for (int x2 = 1; x2 <= n; ++x2)
for (int y2 = 1; y2 <= m; ++y2)
dp[x1][y1][x2][y2] = 1e9;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int a;
cin >> a;
dp[i][j][i][j] = 0;
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + a;
}
}
for (int x2 = 1; x2 <= n; ++x2) {
for (int y2 = 1; y2 <= m; ++y2) {
for (int x1 = x2; x1 >= 1; --x1) {
for (int y1 = y2; y1 >= 1; --y1) {
for (int k = x1; k < x2; ++k)
dp[x1][y1][x2][y2] = min(dp[x1][y1][k][y2] + dp[k + 1][y1][x2][y2] + sum(x1, y1, x2, y2), dp[x1][y1][x2][y2]);
for (int k = y1; k < y2; ++k)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][k] + dp[x1][k + 1][x2][y2] + sum(x1, y1, x2, y2), dp[x1][y1][x2][y2]);
//printf("(%d, %d), (%d, %d) %d\n", x1, y1, x2, y2, dp[x1][y1][x2][y2]);
}
}
}
}
cout << dp[1][1][n][m] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
3 ms |
3532 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
6 ms |
6204 KB |
Output is correct |
11 |
Correct |
5 ms |
5180 KB |
Output is correct |
12 |
Correct |
18 ms |
11300 KB |
Output is correct |
13 |
Correct |
28 ms |
14668 KB |
Output is correct |
14 |
Correct |
12 ms |
6488 KB |
Output is correct |
15 |
Correct |
49 ms |
16524 KB |
Output is correct |
16 |
Correct |
5 ms |
5708 KB |
Output is correct |
17 |
Correct |
15 ms |
11624 KB |
Output is correct |
18 |
Correct |
97 ms |
24928 KB |
Output is correct |
19 |
Correct |
166 ms |
30340 KB |
Output is correct |
20 |
Correct |
247 ms |
32244 KB |
Output is correct |