#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define intt long long
#define pb push_back
vector<vector<int>> mat;
int n ;
const intt INF = 1e18;
vector<vector<intt>> dp , dp_;
void solve(int l , int r)
{
if(l + 1 == r)
{
dp[l][r] = dp[r][l] = mat[l][r];
return ;
}
int m = (l + r)/2;
solve(l , m);
solve(m + 1, r);
for(int i = l ; i <= m ; i++)
{
for(int j1 = m + 1 ; j1 <= (m + 1 + r)/2 ; j1++)
{
for(int j2 = (m + 1 + r)/2 + 1; j2 <= r ; j2++)
{
dp_[i][j1] = min(dp_[i][j1] , dp[j1][j2] + mat[j2][i]);
dp_[i][j2] = min(dp_[i][j2] , dp[j1][j2] + mat[j1][i]);
}
}
}
for(int i1 = l ; i1 <= (l + m)/2 ; i1++)
{
for(int i2 = (l + m)/2 + 1 ; i2 <= m ; i2++)
{
for(int j = m + 1 ; j <= r ; j++)
{
dp[i1][j] = min(dp[i1][j] , dp_[i2][j] + dp[i1][i2]);
dp[i2][j] = min(dp[i2][j] , dp_[i1][j] + dp[i1][i2]);
}
}
}
for(int i = l ; i <= m ; i++)
{
for(int j = m + 1 ; j <= r ; j++)
{
dp[j][i] = dp[i][j];
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
mat = vector<vector<int>> (n , vector<int>(n));
dp.assign(n , vector<intt>(n , INF));
dp_ = dp;
for(int i = 0 ; i < n ; i++)
{
for(int j = 0 ; j < n ; j++)
{
cin>>mat[i][j];
}
}
if(n == 1)
{
cout<<0<<'\n';
return 0;
}
intt ans = INF;
solve(0 ,n - 1);
int l = 0 , r = n - 1;
int m = (l + r)/2;
for(int fr = l ; fr <= m ; fr++)
{
for(int bk = m + 1 ; bk <= r ; bk++)
{
ans = min({ans , dp[fr][bk] , dp[bk][fr]});
}
}
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
908 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
952 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
728 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
908 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
952 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
728 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
36 ms |
8172 KB |
Output is correct |
42 |
Correct |
33 ms |
7924 KB |
Output is correct |
43 |
Correct |
34 ms |
8028 KB |
Output is correct |
44 |
Correct |
30 ms |
8028 KB |
Output is correct |
45 |
Correct |
30 ms |
7928 KB |
Output is correct |
46 |
Correct |
31 ms |
8016 KB |
Output is correct |
47 |
Correct |
38 ms |
7936 KB |
Output is correct |
48 |
Correct |
39 ms |
8068 KB |
Output is correct |
49 |
Correct |
30 ms |
8028 KB |
Output is correct |
50 |
Correct |
31 ms |
8060 KB |
Output is correct |
51 |
Correct |
32 ms |
8068 KB |
Output is correct |
52 |
Correct |
30 ms |
8056 KB |
Output is correct |
53 |
Correct |
30 ms |
8056 KB |
Output is correct |
54 |
Correct |
32 ms |
8024 KB |
Output is correct |
55 |
Correct |
36 ms |
8180 KB |
Output is correct |
56 |
Correct |
29 ms |
8028 KB |
Output is correct |
57 |
Correct |
29 ms |
8028 KB |
Output is correct |
58 |
Correct |
30 ms |
8192 KB |
Output is correct |
59 |
Correct |
32 ms |
8200 KB |
Output is correct |
60 |
Correct |
29 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
908 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
952 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
728 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
36 ms |
8172 KB |
Output is correct |
42 |
Correct |
33 ms |
7924 KB |
Output is correct |
43 |
Correct |
34 ms |
8028 KB |
Output is correct |
44 |
Correct |
30 ms |
8028 KB |
Output is correct |
45 |
Correct |
30 ms |
7928 KB |
Output is correct |
46 |
Correct |
31 ms |
8016 KB |
Output is correct |
47 |
Correct |
38 ms |
7936 KB |
Output is correct |
48 |
Correct |
39 ms |
8068 KB |
Output is correct |
49 |
Correct |
30 ms |
8028 KB |
Output is correct |
50 |
Correct |
31 ms |
8060 KB |
Output is correct |
51 |
Correct |
32 ms |
8068 KB |
Output is correct |
52 |
Correct |
30 ms |
8056 KB |
Output is correct |
53 |
Correct |
30 ms |
8056 KB |
Output is correct |
54 |
Correct |
32 ms |
8024 KB |
Output is correct |
55 |
Correct |
36 ms |
8180 KB |
Output is correct |
56 |
Correct |
29 ms |
8028 KB |
Output is correct |
57 |
Correct |
29 ms |
8028 KB |
Output is correct |
58 |
Correct |
30 ms |
8192 KB |
Output is correct |
59 |
Correct |
32 ms |
8200 KB |
Output is correct |
60 |
Correct |
29 ms |
8028 KB |
Output is correct |
61 |
Correct |
1130 ms |
122996 KB |
Output is correct |
62 |
Correct |
1105 ms |
123164 KB |
Output is correct |
63 |
Correct |
1089 ms |
123168 KB |
Output is correct |
64 |
Correct |
1115 ms |
123144 KB |
Output is correct |
65 |
Correct |
1127 ms |
122968 KB |
Output is correct |
66 |
Correct |
1163 ms |
123164 KB |
Output is correct |
67 |
Correct |
1081 ms |
122952 KB |
Output is correct |
68 |
Correct |
1140 ms |
123092 KB |
Output is correct |
69 |
Correct |
1175 ms |
122944 KB |
Output is correct |
70 |
Correct |
1224 ms |
122948 KB |
Output is correct |
71 |
Correct |
1137 ms |
123804 KB |
Output is correct |
72 |
Correct |
1091 ms |
123804 KB |
Output is correct |
73 |
Correct |
1124 ms |
123844 KB |
Output is correct |
74 |
Correct |
1107 ms |
123640 KB |
Output is correct |
75 |
Correct |
1109 ms |
123908 KB |
Output is correct |
76 |
Correct |
1207 ms |
123800 KB |
Output is correct |
77 |
Correct |
1075 ms |
123788 KB |
Output is correct |
78 |
Correct |
1151 ms |
123988 KB |
Output is correct |
79 |
Correct |
1108 ms |
123804 KB |
Output is correct |
80 |
Correct |
1085 ms |
123984 KB |
Output is correct |