#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll add(ll x, ll y) {
if (LLONG_MAX - x <= y) return LLONG_MAX;
return x + y;
}
vector<vector<ll>> operator*(vector<vector<ll>> &a, vector<vector<ll>> &b){
vector<vector<ll>> c(a.size(), vector<ll> (a.size(), LLONG_MAX));
for (int i=0; i<a.size(); i++){
for (int j=0; j<a.size(); j++){
for (int k=0; k<a.size(); k++){
c[i][j] = min(c[i][j], add(a[i][k], b[k][j]));
}
}
}
return c;
}
vector<vector<ll>> binpow(vector<vector<ll>> a, int k){
vector<vector<ll>> r(a.size(), vector<ll> (a.size(), LLONG_MAX));
for (int i=0; i<a.size(); i++) r[i][i] = 0;
for (; k; k >>= 1, a = a*a)
if (k&1) r = r*a;
return r;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<vector<ll>> a(n, vector<ll> (n, LLONG_MAX));
for (int i=0; i<m; i++){
ll u, v, t; cin >> u >> v >> t;
a[u-1][v-1] = min(a[u-1][v-1], t);
}
for (int i=0; i<n; i++) a[i][i] = 0;
int k; cin >> k;
a = binpow(a, k);
int q; cin >> q;
while (q--){
int u, v; cin >> u >> v;
ll r = a[u-1][v-1];
if (r == LLONG_MAX){
cout << "-1\n";
}
else{
cout << r << "\n";
}
}
}
Compilation message
Main.cpp: In function 'std::vector<std::vector<long long int> > operator*(std::vector<std::vector<long long int> >&, std::vector<std::vector<long long int> >&)':
Main.cpp:10:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i=0; i<a.size(); i++){
| ~^~~~~~~~~
Main.cpp:11:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for (int j=0; j<a.size(); j++){
| ~^~~~~~~~~
Main.cpp:12:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (int k=0; k<a.size(); k++){
| ~^~~~~~~~~
Main.cpp: In function 'std::vector<std::vector<long long int> > binpow(std::vector<std::vector<long long int> >, int)':
Main.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i=0; i<a.size(); i++) r[i][i] = 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
480 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
5 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
6 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
5 ms |
468 KB |
Output is correct |
11 |
Correct |
6 ms |
468 KB |
Output is correct |
12 |
Correct |
8 ms |
468 KB |
Output is correct |
13 |
Correct |
85 ms |
468 KB |
Output is correct |
14 |
Correct |
86 ms |
464 KB |
Output is correct |
15 |
Correct |
95 ms |
468 KB |
Output is correct |
16 |
Correct |
84 ms |
460 KB |
Output is correct |
17 |
Correct |
84 ms |
468 KB |
Output is correct |
18 |
Correct |
85 ms |
468 KB |
Output is correct |
19 |
Correct |
88 ms |
468 KB |
Output is correct |
20 |
Correct |
88 ms |
468 KB |
Output is correct |
21 |
Correct |
83 ms |
460 KB |
Output is correct |
22 |
Correct |
85 ms |
472 KB |
Output is correct |
23 |
Correct |
84 ms |
468 KB |
Output is correct |
24 |
Correct |
85 ms |
480 KB |
Output is correct |
25 |
Correct |
85 ms |
472 KB |
Output is correct |
26 |
Correct |
86 ms |
352 KB |
Output is correct |
27 |
Correct |
87 ms |
352 KB |
Output is correct |
28 |
Correct |
90 ms |
464 KB |
Output is correct |
29 |
Correct |
84 ms |
464 KB |
Output is correct |
30 |
Correct |
87 ms |
480 KB |
Output is correct |
31 |
Correct |
86 ms |
468 KB |
Output is correct |
32 |
Correct |
85 ms |
468 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
480 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
4 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
5 ms |
468 KB |
Output is correct |
14 |
Correct |
5 ms |
468 KB |
Output is correct |
15 |
Correct |
6 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
468 KB |
Output is correct |
18 |
Correct |
6 ms |
468 KB |
Output is correct |
19 |
Correct |
8 ms |
468 KB |
Output is correct |
20 |
Correct |
85 ms |
468 KB |
Output is correct |
21 |
Correct |
86 ms |
464 KB |
Output is correct |
22 |
Correct |
95 ms |
468 KB |
Output is correct |
23 |
Correct |
84 ms |
460 KB |
Output is correct |
24 |
Correct |
84 ms |
468 KB |
Output is correct |
25 |
Correct |
85 ms |
468 KB |
Output is correct |
26 |
Correct |
88 ms |
468 KB |
Output is correct |
27 |
Correct |
88 ms |
468 KB |
Output is correct |
28 |
Correct |
83 ms |
460 KB |
Output is correct |
29 |
Correct |
85 ms |
472 KB |
Output is correct |
30 |
Correct |
84 ms |
468 KB |
Output is correct |
31 |
Correct |
85 ms |
480 KB |
Output is correct |
32 |
Correct |
85 ms |
472 KB |
Output is correct |
33 |
Correct |
86 ms |
352 KB |
Output is correct |
34 |
Correct |
87 ms |
352 KB |
Output is correct |
35 |
Correct |
90 ms |
464 KB |
Output is correct |
36 |
Correct |
84 ms |
464 KB |
Output is correct |
37 |
Correct |
87 ms |
480 KB |
Output is correct |
38 |
Correct |
86 ms |
468 KB |
Output is correct |
39 |
Correct |
85 ms |
468 KB |
Output is correct |
40 |
Correct |
5 ms |
468 KB |
Output is correct |
41 |
Correct |
21 ms |
488 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
43 |
Correct |
19 ms |
468 KB |
Output is correct |
44 |
Correct |
6 ms |
468 KB |
Output is correct |
45 |
Correct |
23 ms |
468 KB |
Output is correct |
46 |
Correct |
82 ms |
464 KB |
Output is correct |
47 |
Correct |
86 ms |
464 KB |
Output is correct |
48 |
Correct |
83 ms |
464 KB |
Output is correct |
49 |
Correct |
100 ms |
464 KB |
Output is correct |
50 |
Correct |
83 ms |
464 KB |
Output is correct |
51 |
Correct |
83 ms |
464 KB |
Output is correct |
52 |
Correct |
84 ms |
348 KB |
Output is correct |
53 |
Correct |
101 ms |
352 KB |
Output is correct |
54 |
Correct |
82 ms |
464 KB |
Output is correct |
55 |
Correct |
85 ms |
484 KB |
Output is correct |
56 |
Correct |
88 ms |
468 KB |
Output is correct |
57 |
Correct |
103 ms |
476 KB |
Output is correct |
58 |
Correct |
85 ms |
468 KB |
Output is correct |
59 |
Correct |
86 ms |
348 KB |
Output is correct |
60 |
Correct |
91 ms |
348 KB |
Output is correct |
61 |
Correct |
102 ms |
352 KB |
Output is correct |
62 |
Correct |
82 ms |
484 KB |
Output is correct |
63 |
Correct |
85 ms |
460 KB |
Output is correct |
64 |
Correct |
87 ms |
468 KB |
Output is correct |
65 |
Correct |
102 ms |
460 KB |
Output is correct |