/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 3000+100, M = 1e5+10, K = 52, MX = 30;
const ll INF = 1e18;
int n;
ll c[N][N];
ll dp[N][N];
array<ll, 4> pos[N];
void f(int l, int r, int v){
// for(int i = 1; i <= r - l + 1; ++i) dp[v][i].resize(r - l + 2, INF);
if(l == r){
dp[l][l] = 0;
pos[v] = {l, l, l, l};
return;
}
int mid = l+r>>1;
f(l, mid, v<<1);
f(mid+1, r, v<<1|1);
int S = mid - l + 1;
for(int x = pos[v<<1][0]; x <= pos[v<<1][1]; ++x){
for(int y = pos[v<<1][2]; y <= pos[v<<1][3]; ++y){
for(int rep = 0; rep < 2; ++rep){
for(int X = pos[v<<1|1][0]; X <= pos[v<<1|1][1]; ++X){
for(int Y = pos[v<<1|1][2]; Y <= pos[v<<1|1][3]; ++Y){
for(int rp = 0; rp < 2; ++rp){
dp[x][Y] = min(dp[x][Y], dp[x][y] + dp[X][Y] + c[y][X]);
dp[X][y] = min(dp[X][y], dp[x][y] + dp[X][Y] + c[x][Y]);
swap(X, Y);
}
}
}
swap(x, y);
}
}
}
pos[v] = {l, mid, mid + 1, r};
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j) cin >> c[i][j];
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j) dp[i][j] = INF;
}
f(1, n, 1);
ll ans = INF;
for(int rep = 0; rep < 2; ++rep){
for(int i = pos[1][0]; i <= pos[1][1]; ++i){
for(int j = pos[1][2]; j <= pos[1][3]; ++j){
ans = min(ans, dp[i][j]);
}
}
swap(pos[1][0], pos[1][2]);
swap(pos[1][1], pos[1][3]);
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | 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... |