#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define pb push_back
#define se second
#define fi first
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int nax=2050;
int cost [nax][nax];
long long dp1[nax][nax];
long long dp[nax][nax];
void solve(int l,int r){
if(l+1==r){
dp[l][r]=dp[r][l]=cost[l][r];
return;
}
int mid=(r+l)/2;
solve(l,mid);
solve(mid+1,r);
for (int i = l; i <= mid; ++i)
{
for (int j1 = mid+1; j1 <= (mid+1+r)/2; ++j1)
{
for (int j2 = (mid+1+r)/2+1; j2 <= r; ++j2)
{
dp1[i][j1]=min(dp1[i][j1],dp[j1][j2]+cost[i][j2]);
dp1[i][j2]=min(dp1[i][j2],dp[j1][j2]+cost[i][j1]);
}
}
}
for (int i1 = l; i1 <= (l+mid)/2; ++i1)
{
for (int i2 = (l+mid)/2+1; i2 <= mid; ++i2)
{
for (int j = mid+1; j <= r; ++j)
{
dp[i1][j]=min(dp[i1][j],dp1[i2][j]+dp[i1][i2]);
dp[i2][j]=min(dp[i2][j],dp1[i1][j]+dp[i1][i2]);
}
}
}
for (int i = l; i <= mid; ++i)
{
for (int j = mid+1; j <= r; ++j)
{
dp[j][i]=dp[i][j];
}
}
}
int main() {/*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
optimise;
int n;
cin>>n;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
dp[i][j]=dp1[i][j]=1e18;
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin>>cost[i][j];
}
}
long long ans=1e18;
solve(0,n-1);
for (int i = 0; i <= (n-1)/2; ++i)
{
for (int j = (n-1)/2+1; j <= n-1; ++j)
{
ans=min(ans,min(dp[i][j],dp[j][i]));
}
}
cout <<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4856 KB |
Output is correct |
12 |
Correct |
0 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
0 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4856 KB |
Output is correct |
12 |
Correct |
0 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
0 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
4188 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
6236 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
2 ms |
4188 KB |
Output is correct |
29 |
Correct |
3 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
4188 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
4 ms |
6236 KB |
Output is correct |
34 |
Correct |
2 ms |
6236 KB |
Output is correct |
35 |
Correct |
2 ms |
4188 KB |
Output is correct |
36 |
Correct |
3 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
6236 KB |
Output is correct |
39 |
Correct |
2 ms |
4188 KB |
Output is correct |
40 |
Correct |
2 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4856 KB |
Output is correct |
12 |
Correct |
0 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
0 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
4188 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
6236 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
2 ms |
4188 KB |
Output is correct |
29 |
Correct |
3 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
4188 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
4 ms |
6236 KB |
Output is correct |
34 |
Correct |
2 ms |
6236 KB |
Output is correct |
35 |
Correct |
2 ms |
4188 KB |
Output is correct |
36 |
Correct |
3 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
6236 KB |
Output is correct |
39 |
Correct |
2 ms |
4188 KB |
Output is correct |
40 |
Correct |
2 ms |
4188 KB |
Output is correct |
41 |
Correct |
34 ms |
15704 KB |
Output is correct |
42 |
Correct |
38 ms |
15708 KB |
Output is correct |
43 |
Correct |
34 ms |
13660 KB |
Output is correct |
44 |
Correct |
37 ms |
15612 KB |
Output is correct |
45 |
Correct |
36 ms |
13656 KB |
Output is correct |
46 |
Correct |
41 ms |
15748 KB |
Output is correct |
47 |
Correct |
35 ms |
13660 KB |
Output is correct |
48 |
Correct |
37 ms |
15632 KB |
Output is correct |
49 |
Correct |
34 ms |
15708 KB |
Output is correct |
50 |
Correct |
34 ms |
15616 KB |
Output is correct |
51 |
Correct |
34 ms |
15528 KB |
Output is correct |
52 |
Correct |
34 ms |
15700 KB |
Output is correct |
53 |
Correct |
40 ms |
13652 KB |
Output is correct |
54 |
Correct |
34 ms |
15704 KB |
Output is correct |
55 |
Correct |
34 ms |
15732 KB |
Output is correct |
56 |
Correct |
37 ms |
13660 KB |
Output is correct |
57 |
Correct |
33 ms |
15708 KB |
Output is correct |
58 |
Correct |
34 ms |
13996 KB |
Output is correct |
59 |
Correct |
34 ms |
15720 KB |
Output is correct |
60 |
Correct |
34 ms |
13660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4856 KB |
Output is correct |
12 |
Correct |
0 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
0 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
4188 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
2 ms |
6236 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
6236 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
2 ms |
4188 KB |
Output is correct |
29 |
Correct |
3 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
4188 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
4 ms |
6236 KB |
Output is correct |
34 |
Correct |
2 ms |
6236 KB |
Output is correct |
35 |
Correct |
2 ms |
4188 KB |
Output is correct |
36 |
Correct |
3 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
6236 KB |
Output is correct |
39 |
Correct |
2 ms |
4188 KB |
Output is correct |
40 |
Correct |
2 ms |
4188 KB |
Output is correct |
41 |
Correct |
34 ms |
15704 KB |
Output is correct |
42 |
Correct |
38 ms |
15708 KB |
Output is correct |
43 |
Correct |
34 ms |
13660 KB |
Output is correct |
44 |
Correct |
37 ms |
15612 KB |
Output is correct |
45 |
Correct |
36 ms |
13656 KB |
Output is correct |
46 |
Correct |
41 ms |
15748 KB |
Output is correct |
47 |
Correct |
35 ms |
13660 KB |
Output is correct |
48 |
Correct |
37 ms |
15632 KB |
Output is correct |
49 |
Correct |
34 ms |
15708 KB |
Output is correct |
50 |
Correct |
34 ms |
15616 KB |
Output is correct |
51 |
Correct |
34 ms |
15528 KB |
Output is correct |
52 |
Correct |
34 ms |
15700 KB |
Output is correct |
53 |
Correct |
40 ms |
13652 KB |
Output is correct |
54 |
Correct |
34 ms |
15704 KB |
Output is correct |
55 |
Correct |
34 ms |
15732 KB |
Output is correct |
56 |
Correct |
37 ms |
13660 KB |
Output is correct |
57 |
Correct |
33 ms |
15708 KB |
Output is correct |
58 |
Correct |
34 ms |
13996 KB |
Output is correct |
59 |
Correct |
34 ms |
15720 KB |
Output is correct |
60 |
Correct |
34 ms |
13660 KB |
Output is correct |
61 |
Correct |
1053 ms |
82436 KB |
Output is correct |
62 |
Correct |
1115 ms |
122968 KB |
Output is correct |
63 |
Correct |
1035 ms |
122912 KB |
Output is correct |
64 |
Correct |
1073 ms |
122964 KB |
Output is correct |
65 |
Correct |
1034 ms |
122960 KB |
Output is correct |
66 |
Correct |
1147 ms |
123024 KB |
Output is correct |
67 |
Correct |
1093 ms |
122976 KB |
Output is correct |
68 |
Correct |
1083 ms |
123048 KB |
Output is correct |
69 |
Correct |
1157 ms |
122960 KB |
Output is correct |
70 |
Correct |
1132 ms |
122984 KB |
Output is correct |
71 |
Correct |
1165 ms |
123480 KB |
Output is correct |
72 |
Correct |
1176 ms |
123684 KB |
Output is correct |
73 |
Correct |
1202 ms |
123588 KB |
Output is correct |
74 |
Correct |
1196 ms |
123692 KB |
Output is correct |
75 |
Correct |
1148 ms |
123440 KB |
Output is correct |
76 |
Correct |
1179 ms |
123472 KB |
Output is correct |
77 |
Correct |
1144 ms |
123648 KB |
Output is correct |
78 |
Correct |
1055 ms |
123696 KB |
Output is correct |
79 |
Correct |
1112 ms |
123688 KB |
Output is correct |
80 |
Correct |
1160 ms |
123432 KB |
Output is correct |