#include<bits/stdc++.h>
using namespace std;
int N, M, Q, K;
vector<vector<long long int>> states;
vector<vector<long long int>> AdjMatrix;
void preprocess(){
bool changed = true;
vector<vector<long long int>> newState(N, vector<long long int>(N));
vector<vector<int>> hasBeenChanged(N, vector<int>(N, 0));
for(int i = 0; i < N; ++i) hasBeenChanged[i][i] = 1;
vector<vector<int>> nextToBeChanged(N, vector<int>(N, 0));
for(int i = 0; i < K and changed; ++i){
newState = states;
changed = false;
nextToBeChanged = vector<vector<int>>(N, vector<int>(N, 0));
for(int nodeFrom = 0; nodeFrom < N; ++nodeFrom){
for(int nodeTo = 0; nodeTo < N; ++nodeTo){
if(nodeFrom == nodeTo and i > 0) continue;
if(!hasBeenChanged[nodeFrom][nodeTo]) continue;
for(int destination = 0; destination < N; ++destination){
if(destination == nodeTo) continue;
long long int dist = (long long)states[nodeFrom][nodeTo] + (long long)AdjMatrix[nodeTo][destination];
int nextNode = destination;
//cout << "Iteration: " << i+1 << "\t From: " << nodeFrom << " and " << nodeTo << " to " << nextNode << " distance " << dist << " versus " << states[nodeFrom][nextNode] << endl;
if(states[nodeFrom][nextNode] > dist){
changed = true;
newState[nodeFrom][nextNode] = min(newState[nodeFrom][nextNode], dist);
nextToBeChanged[nodeFrom][nextNode] = 1;
}
}
}
}
states = newState;
hasBeenChanged = nextToBeChanged;
}
}
int main(){
cin >> N >> M;
AdjMatrix = vector<vector<long long int>>(N, vector<long long int>(N, 1e18));
states = vector<vector<long long int>>(N, vector<long long int>(N, 1e18));
for(int i = 0; i < N; ++i) states[i][i] = 0;
for(int i = 0; i < N; ++i) AdjMatrix[i][i] = 0;
int a, b;
long long w;
for(int i = 0; i < M; ++i){
cin >> a >> b >> w;
a--; b--;
AdjMatrix[a][b] = min(AdjMatrix[a][b], w);
}
cin >> K >> Q;
preprocess();
while(Q--){
cin >> a >> b;
a--; b--;
if(states[a][b] == 1e18) cout << -1 << endl;
else cout << states[a][b] << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
504 KB |
Output is correct |
3 |
Correct |
9 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
496 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
6 |
Correct |
14 ms |
492 KB |
Output is correct |
7 |
Correct |
13 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
10 ms |
504 KB |
Output is correct |
8 |
Correct |
10 ms |
440 KB |
Output is correct |
9 |
Correct |
10 ms |
532 KB |
Output is correct |
10 |
Correct |
10 ms |
536 KB |
Output is correct |
11 |
Correct |
13 ms |
568 KB |
Output is correct |
12 |
Correct |
14 ms |
468 KB |
Output is correct |
13 |
Correct |
298 ms |
868 KB |
Output is correct |
14 |
Correct |
293 ms |
872 KB |
Output is correct |
15 |
Correct |
290 ms |
940 KB |
Output is correct |
16 |
Correct |
283 ms |
948 KB |
Output is correct |
17 |
Correct |
293 ms |
868 KB |
Output is correct |
18 |
Correct |
290 ms |
900 KB |
Output is correct |
19 |
Correct |
289 ms |
996 KB |
Output is correct |
20 |
Correct |
289 ms |
904 KB |
Output is correct |
21 |
Correct |
291 ms |
972 KB |
Output is correct |
22 |
Correct |
301 ms |
1000 KB |
Output is correct |
23 |
Correct |
291 ms |
972 KB |
Output is correct |
24 |
Correct |
295 ms |
960 KB |
Output is correct |
25 |
Correct |
316 ms |
872 KB |
Output is correct |
26 |
Correct |
302 ms |
964 KB |
Output is correct |
27 |
Correct |
294 ms |
1056 KB |
Output is correct |
28 |
Correct |
296 ms |
972 KB |
Output is correct |
29 |
Correct |
290 ms |
876 KB |
Output is correct |
30 |
Correct |
294 ms |
936 KB |
Output is correct |
31 |
Correct |
319 ms |
964 KB |
Output is correct |
32 |
Correct |
307 ms |
6504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
8 ms |
468 KB |
Output is correct |
8 |
Correct |
9 ms |
504 KB |
Output is correct |
9 |
Correct |
9 ms |
468 KB |
Output is correct |
10 |
Correct |
9 ms |
496 KB |
Output is correct |
11 |
Correct |
10 ms |
468 KB |
Output is correct |
12 |
Correct |
14 ms |
492 KB |
Output is correct |
13 |
Correct |
13 ms |
492 KB |
Output is correct |
14 |
Correct |
10 ms |
504 KB |
Output is correct |
15 |
Correct |
10 ms |
440 KB |
Output is correct |
16 |
Correct |
10 ms |
532 KB |
Output is correct |
17 |
Correct |
10 ms |
536 KB |
Output is correct |
18 |
Correct |
13 ms |
568 KB |
Output is correct |
19 |
Correct |
14 ms |
468 KB |
Output is correct |
20 |
Correct |
298 ms |
868 KB |
Output is correct |
21 |
Correct |
293 ms |
872 KB |
Output is correct |
22 |
Correct |
290 ms |
940 KB |
Output is correct |
23 |
Correct |
283 ms |
948 KB |
Output is correct |
24 |
Correct |
293 ms |
868 KB |
Output is correct |
25 |
Correct |
290 ms |
900 KB |
Output is correct |
26 |
Correct |
289 ms |
996 KB |
Output is correct |
27 |
Correct |
289 ms |
904 KB |
Output is correct |
28 |
Correct |
291 ms |
972 KB |
Output is correct |
29 |
Correct |
301 ms |
1000 KB |
Output is correct |
30 |
Correct |
291 ms |
972 KB |
Output is correct |
31 |
Correct |
295 ms |
960 KB |
Output is correct |
32 |
Correct |
316 ms |
872 KB |
Output is correct |
33 |
Correct |
302 ms |
964 KB |
Output is correct |
34 |
Correct |
294 ms |
1056 KB |
Output is correct |
35 |
Correct |
296 ms |
972 KB |
Output is correct |
36 |
Correct |
290 ms |
876 KB |
Output is correct |
37 |
Correct |
294 ms |
936 KB |
Output is correct |
38 |
Correct |
319 ms |
964 KB |
Output is correct |
39 |
Correct |
307 ms |
6504 KB |
Output is correct |
40 |
Correct |
11 ms |
440 KB |
Output is correct |
41 |
Correct |
11 ms |
440 KB |
Output is correct |
42 |
Correct |
13 ms |
536 KB |
Output is correct |
43 |
Correct |
12 ms |
532 KB |
Output is correct |
44 |
Correct |
14 ms |
468 KB |
Output is correct |
45 |
Correct |
16 ms |
568 KB |
Output is correct |
46 |
Correct |
313 ms |
6608 KB |
Output is correct |
47 |
Correct |
317 ms |
6504 KB |
Output is correct |
48 |
Correct |
311 ms |
6632 KB |
Output is correct |
49 |
Correct |
302 ms |
6612 KB |
Output is correct |
50 |
Correct |
314 ms |
6628 KB |
Output is correct |
51 |
Correct |
297 ms |
6620 KB |
Output is correct |
52 |
Correct |
313 ms |
6880 KB |
Output is correct |
53 |
Correct |
303 ms |
6624 KB |
Output is correct |
54 |
Correct |
301 ms |
6640 KB |
Output is correct |
55 |
Correct |
312 ms |
6624 KB |
Output is correct |
56 |
Correct |
303 ms |
6636 KB |
Output is correct |
57 |
Correct |
310 ms |
6884 KB |
Output is correct |
58 |
Correct |
318 ms |
6760 KB |
Output is correct |
59 |
Correct |
315 ms |
6828 KB |
Output is correct |
60 |
Correct |
322 ms |
6732 KB |
Output is correct |
61 |
Correct |
322 ms |
6756 KB |
Output is correct |
62 |
Correct |
297 ms |
6504 KB |
Output is correct |
63 |
Correct |
331 ms |
6628 KB |
Output is correct |
64 |
Correct |
346 ms |
6500 KB |
Output is correct |
65 |
Correct |
300 ms |
6560 KB |
Output is correct |