#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int n;
ll arr[2050][2050];
ll DP[2050][2050];
ll mid[2050][2050];
ll ans;
void dfs(int l, int r){
if(r-l==1){
DP[l][r] = DP[r][l] = arr[l][r];
return;
}
int m = (l+r)>>1;
dfs(l, m);
dfs(m+1, r);
int m1 = (l+m)/2;
for(int i=l; i<=m; i++) for(int j=l; j<=m; j++) for(int k=m+1; k<=r; k++){
if((i<=m1) == (j<=m1)) continue;
mid[i][k] = min(mid[i][k], DP[i][j] + arr[j][k]);
}
int m2 = (m+1+r)/2;
for(int i=l; i<=m; i++) for(int j=m+1; j<=r; j++) for(int k=m+1; k<=r; k++){
if((j<=m2) == (k<=m2)) continue;
DP[k][i] = DP[i][k] = min(DP[i][k], mid[i][j] + DP[j][k]);
}
}
ll L[2050], R[2050];
int main(){
scanf("%d", &n);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
scanf("%lld", &arr[i][j]);
DP[i][j] = mid[i][j] = INF;
}
}
if(n<=4){
dfs(1, n);
ans = INF;
for(int i=1; i<=n/2; i++) for(int j=n/2+1; j<=n; j++) ans = min(ans, DP[i][j]);
printf("%lld", ans);
return 0;
}
int m = n/2;
int m1 = (1+m)/2, m2 = (m+1+n)/2;
dfs(1, m);
dfs(m+1, n);
for(int i=1; i<=n; i++) L[i] = R[i] = INF;
for(int i=1; i<=m; i++){
for(int j=1; j<=m; j++){
if((i<=m1) == (j<=m1)) continue;
L[i] = min(L[i], DP[i][j]);
}
}
for(int i=m+1; i<=n; i++){
for(int j=m+1; j<=n; j++){
if((i<=m2) == (j<=m2)) continue;
R[i] = min(R[i], DP[i][j]);
}
}
ll ans = INF;
for(int i=1; i<=m; i++) for(int j=m+1; j<=n; j++) ans = min(ans, L[i] + R[j] + arr[i][j]);
printf("%lld", ans);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:41:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%lld", &arr[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
2260 KB |
Output is correct |
22 |
Correct |
3 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1032 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
3 ms |
2264 KB |
Output is correct |
27 |
Correct |
3 ms |
2260 KB |
Output is correct |
28 |
Correct |
3 ms |
2260 KB |
Output is correct |
29 |
Correct |
3 ms |
2192 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
3 ms |
2260 KB |
Output is correct |
32 |
Correct |
3 ms |
2260 KB |
Output is correct |
33 |
Correct |
4 ms |
2260 KB |
Output is correct |
34 |
Correct |
2 ms |
2260 KB |
Output is correct |
35 |
Correct |
3 ms |
2260 KB |
Output is correct |
36 |
Correct |
2 ms |
2260 KB |
Output is correct |
37 |
Correct |
3 ms |
2220 KB |
Output is correct |
38 |
Correct |
2 ms |
2260 KB |
Output is correct |
39 |
Correct |
3 ms |
2260 KB |
Output is correct |
40 |
Correct |
3 ms |
2244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
2260 KB |
Output is correct |
22 |
Correct |
3 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1032 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
3 ms |
2264 KB |
Output is correct |
27 |
Correct |
3 ms |
2260 KB |
Output is correct |
28 |
Correct |
3 ms |
2260 KB |
Output is correct |
29 |
Correct |
3 ms |
2192 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
3 ms |
2260 KB |
Output is correct |
32 |
Correct |
3 ms |
2260 KB |
Output is correct |
33 |
Correct |
4 ms |
2260 KB |
Output is correct |
34 |
Correct |
2 ms |
2260 KB |
Output is correct |
35 |
Correct |
3 ms |
2260 KB |
Output is correct |
36 |
Correct |
2 ms |
2260 KB |
Output is correct |
37 |
Correct |
3 ms |
2220 KB |
Output is correct |
38 |
Correct |
2 ms |
2260 KB |
Output is correct |
39 |
Correct |
3 ms |
2260 KB |
Output is correct |
40 |
Correct |
3 ms |
2244 KB |
Output is correct |
41 |
Correct |
43 ms |
12632 KB |
Output is correct |
42 |
Correct |
37 ms |
12560 KB |
Output is correct |
43 |
Correct |
36 ms |
12652 KB |
Output is correct |
44 |
Correct |
36 ms |
12644 KB |
Output is correct |
45 |
Correct |
35 ms |
12640 KB |
Output is correct |
46 |
Correct |
48 ms |
12636 KB |
Output is correct |
47 |
Correct |
35 ms |
12656 KB |
Output is correct |
48 |
Correct |
37 ms |
12648 KB |
Output is correct |
49 |
Correct |
44 ms |
12588 KB |
Output is correct |
50 |
Correct |
37 ms |
12648 KB |
Output is correct |
51 |
Correct |
34 ms |
12648 KB |
Output is correct |
52 |
Correct |
36 ms |
12628 KB |
Output is correct |
53 |
Correct |
35 ms |
12628 KB |
Output is correct |
54 |
Correct |
36 ms |
12628 KB |
Output is correct |
55 |
Correct |
50 ms |
12632 KB |
Output is correct |
56 |
Correct |
37 ms |
12908 KB |
Output is correct |
57 |
Correct |
36 ms |
13040 KB |
Output is correct |
58 |
Correct |
38 ms |
13004 KB |
Output is correct |
59 |
Correct |
36 ms |
13008 KB |
Output is correct |
60 |
Correct |
38 ms |
13020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
2260 KB |
Output is correct |
22 |
Correct |
3 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1032 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
3 ms |
2264 KB |
Output is correct |
27 |
Correct |
3 ms |
2260 KB |
Output is correct |
28 |
Correct |
3 ms |
2260 KB |
Output is correct |
29 |
Correct |
3 ms |
2192 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
3 ms |
2260 KB |
Output is correct |
32 |
Correct |
3 ms |
2260 KB |
Output is correct |
33 |
Correct |
4 ms |
2260 KB |
Output is correct |
34 |
Correct |
2 ms |
2260 KB |
Output is correct |
35 |
Correct |
3 ms |
2260 KB |
Output is correct |
36 |
Correct |
2 ms |
2260 KB |
Output is correct |
37 |
Correct |
3 ms |
2220 KB |
Output is correct |
38 |
Correct |
2 ms |
2260 KB |
Output is correct |
39 |
Correct |
3 ms |
2260 KB |
Output is correct |
40 |
Correct |
3 ms |
2244 KB |
Output is correct |
41 |
Correct |
43 ms |
12632 KB |
Output is correct |
42 |
Correct |
37 ms |
12560 KB |
Output is correct |
43 |
Correct |
36 ms |
12652 KB |
Output is correct |
44 |
Correct |
36 ms |
12644 KB |
Output is correct |
45 |
Correct |
35 ms |
12640 KB |
Output is correct |
46 |
Correct |
48 ms |
12636 KB |
Output is correct |
47 |
Correct |
35 ms |
12656 KB |
Output is correct |
48 |
Correct |
37 ms |
12648 KB |
Output is correct |
49 |
Correct |
44 ms |
12588 KB |
Output is correct |
50 |
Correct |
37 ms |
12648 KB |
Output is correct |
51 |
Correct |
34 ms |
12648 KB |
Output is correct |
52 |
Correct |
36 ms |
12628 KB |
Output is correct |
53 |
Correct |
35 ms |
12628 KB |
Output is correct |
54 |
Correct |
36 ms |
12628 KB |
Output is correct |
55 |
Correct |
50 ms |
12632 KB |
Output is correct |
56 |
Correct |
37 ms |
12908 KB |
Output is correct |
57 |
Correct |
36 ms |
13040 KB |
Output is correct |
58 |
Correct |
38 ms |
13004 KB |
Output is correct |
59 |
Correct |
36 ms |
13008 KB |
Output is correct |
60 |
Correct |
38 ms |
13020 KB |
Output is correct |
61 |
Correct |
997 ms |
99776 KB |
Output is correct |
62 |
Correct |
1056 ms |
100880 KB |
Output is correct |
63 |
Correct |
1016 ms |
111708 KB |
Output is correct |
64 |
Correct |
974 ms |
100832 KB |
Output is correct |
65 |
Correct |
987 ms |
111972 KB |
Output is correct |
66 |
Correct |
980 ms |
112140 KB |
Output is correct |
67 |
Correct |
982 ms |
101384 KB |
Output is correct |
68 |
Correct |
965 ms |
106912 KB |
Output is correct |
69 |
Correct |
1012 ms |
98912 KB |
Output is correct |
70 |
Correct |
973 ms |
100292 KB |
Output is correct |
71 |
Correct |
973 ms |
101260 KB |
Output is correct |
72 |
Correct |
1000 ms |
111392 KB |
Output is correct |
73 |
Correct |
975 ms |
101724 KB |
Output is correct |
74 |
Correct |
1030 ms |
101800 KB |
Output is correct |
75 |
Correct |
969 ms |
101872 KB |
Output is correct |
76 |
Correct |
987 ms |
101852 KB |
Output is correct |
77 |
Correct |
1006 ms |
101560 KB |
Output is correct |
78 |
Correct |
987 ms |
101672 KB |
Output is correct |
79 |
Correct |
1050 ms |
101652 KB |
Output is correct |
80 |
Correct |
975 ms |
101536 KB |
Output is correct |