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;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 555;
const long long INF = 1e16;
int a[N][N];
long long dp[N][N][N];
void build(int nd, int l, int r) {
if (l + 1 == r) {
dp[nd][l][r] = a[l][r];
dp[nd][r][l] = a[l][r];
return;
}
int mid = (l + r) >> 1;
build(nd * 2, l, mid);
build(nd * 2 + 1, mid + 1, r);
for (int i = l; i <= r; ++i) {
for (int j = l; j <= r; ++j) {
dp[nd][i][j] = INF;
}
}
for (int i = l; i <= mid; ++i) {
for (int j = l; j <= mid; ++j) {
if (i == j) continue;
for (int k = mid + 1; k <= r; ++k) {
for (int m = mid + 1; m <= r; ++m) {
if (k == m) continue;
dp[nd][i][m] = min(dp[nd][i][m], dp[nd * 2][i][j] + dp[nd * 2 + 1][k][m] + a[j][k]);
dp[nd][m][i] = min(dp[nd][i][m], dp[nd * 2][j][i] + dp[nd * 2 + 1][m][k] + a[j][k]);
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> a[i][j];
}
}
build(1, 1, n);
long long ans = INF;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i != j) {
ans = min(ans, dp[1][i][j]);
}
}
}
cout << ans << '\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... |