-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1856.cpp
More file actions
45 lines (39 loc) · 1003 Bytes
/
P1856.cpp
File metadata and controls
45 lines (39 loc) · 1003 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
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
signed main() {
int n;
cin >> n;
vector<vector<int>> d(n+1, vector<int>(n+1,0));
for(int i=1; i<=n; ++i) {
for(int j=1; j<=n; ++j) {
cin >> d[i][j];
}
}
int k;
cin >> k;
vector<long long> results;
while(k--) {
int u, v, w;
cin >> u >> v >> w;
if(w < d[u][v]||(d[u][v]==0&&u!=v)) {
d[u][v] = d[v][u] = w;
for(int i=1; i<=n; ++i) {
for(int j=1; j<=n; ++j) {
d[j][i] = d[i][j] = min(d[i][j], d[i][u] + d[u][j]);
d[j][i] = d[i][j] = min(d[i][j], d[i][v] + d[v][j]);
}
}
}
long long total = 0;
for(int i=1; i<=n; ++i) {
for(int j=i+1; j<=n; ++j) {
total += d[i][j];
}
}
results.push_back(total);
}
for(auto x : results)
cout<<x<<" ";
cout<<endl;
return 0;
}