This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[2049][2049];
const ll inf = 1e18;
void chmin (ll &x, ll y) {
if (x > y) x = y;
}
vector <vector <ll>> recurse (int l, int r) {
int sze = r - l + 1;
vector <vector <ll>> dp(sze, vector <ll> (sze, inf));
if (l == r - 1) {
dp[0][1] = dp[1][0] = a[l][r];
return dp;
}
sze >>= 1;
int mid = (l + r) >> 1;
auto g = recurse(l, mid), h = recurse(1 + mid, r);
for (int i = 0; i < sze; i++) {
for (int j = 0; j < sze; j++) {
ll mn = inf;
for (int k = 0; k < sze; k++) {
chmin(mn, g[i][k] + a[k + l][j + mid + 1]);
}
for (int k = 0; k < sze; k++) {
chmin(dp[i][k + sze], mn + h[j][k]);
}
}
}
sze <<= 1;
for (int i = 0; i < (sze >> 1); i++) for (int j = (sze >> 1); j < sze; j++) dp[j][i] = dp[i][j];
return dp;
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
if (n == 2) {
cout << a[1][2] << '\n';
return 0;
}
auto g = recurse(1, n / 2), h = recurse(n / 2 + 1, n);
ll mn[n / 2 + 1][2];
for (int i = 0; i <= n / 2; i++) {
for (int j = 0; j < 2; j++) {
mn[i][j] = inf;
}
}
ll ans = inf;
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n / 2; j++) {
chmin(mn[i][0], g[i][j]);
chmin(mn[i][1], h[i][j]);
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n / 2; j++) {
chmin(ans, mn[i][0] + mn[j][1] + a[i + 1][j + n / 2 + 1]);
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |