-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
41 lines (35 loc) · 797 Bytes
/
Copy pathB.cpp
File metadata and controls
41 lines (35 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline int len(const T &a) { return a.size(); }
constexpr int N = 5e2 + 10;
int n, x[N];
bool added[N];
long long dist[N][N];
long long ans[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%lld", &dist[i][j]);
}
}
for (int i = 0; i < n; ++i) {
scanf("%d", &x[i]);
--x[i];
}
for (int k = n - 1; k >= 0; --k) {
added[x[k]] = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][x[k]] + dist[x[k]][j]);
if (added[i] && added[j]) {
ans[k] += dist[i][j];
}
}
}
}
for (int i = 0; i < n; ++i) {
printf("%lld ", ans[i]);
}
return 0;
}