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>
#define pb push_back
#define int long long
using namespace std;
const int inf = 1e15 + 10;
const int N = 2048 + 10;
vector<vector<int> > c(N, vector<int>(N));
vector<vector<int> > dp[N];
int ans;
void f(int node, int l, int r){
if(r - l == 1){
dp[node].resize(2);
dp[node][0].resize(2);
dp[node][1].resize(2);
dp[node][0][1] = dp[node][1][0] = c[l][r];
dp[node][0][0] = dp[node][1][1] = inf;
return;
}
int m = (l + r)/2;
f(node * 2, l, m);
f(node * 2 + 1, m+1, r);
int sz = (r - l + 1)/2;
if(node == 1){
vector<int> ldp(sz, inf), rdp(sz, inf);
for(int i = 0; i < sz; i++){
for(int j = 0; j < sz; j++){
ldp[j] = min(ldp[j], dp[node*2][i][j]);
}
}
for(int i = 0; i < sz; i++){
for(int j = 0; j < sz; j++){
rdp[i] = min(rdp[i], dp[node*2+1][i][j]);
}
}
ans = inf;
for(int i = 0; i < sz; i++){
for(int j = 0; j < sz; j++){
ans = min(ans, ldp[j] + c[j + l][i + l + sz] + rdp[i]);
}
}
return;
}
dp[node].resize(sz*2);
for(int i = 0; i < sz*2; i++){
dp[node][i].resize(sz*2);
}
for(int i = 0; i < sz*2; i++){
for(int j = 0; j < sz*2; j++){
dp[node][i][j] = inf;
}
}
vector<vector<int> > dp2(sz, vector<int>(sz, inf));
for(int l1 = 0; l1 < sz; l1++){
for(int r1 = 0; r1 < sz; r1++){
for(int l2 = 0; l2 < sz; l2++){
dp2[l1][l2] = min(dp2[l1][l2], dp[node*2][l1][r1] + c[r1 + l][l2 + l + sz]);
}
}
}
for(int l1 = 0; l1 < sz; l1++){
for(int r2 = 0; r2 < sz; r2++){
for(int l2 = 0; l2 < sz; l2++){
dp[node][l1][r2 + sz] = min(dp[node][l1][r2 + sz], dp2[l1][l2] + dp[node*2+1][l2][r2]);
}
for(int r1 = 0; r1 < sz; r1++){
dp[node][l1 + sz][r2] = min(dp[node][l1 + sz][r2], dp[node*2+1][l1][r1] + dp2[l1][r2]); // sag taraf l1, r1 -> oradan da l2, r2 gecicez
}
}
}
dp[node*2].clear();
dp[node*2+1].clear();
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
//c[i][j] = 1;
cin>>c[i][j];
}
}
if(n == 1){
cout<<0<<endl;
return 0;
}
f(1, 0, n-1);
cout<<ans<<endl;
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... |