#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
#define int long long
const int inf = 1e18;
struct Matrix{
vector<vector<int>> v;
int r, c;
Matrix() = default;
Matrix(int R): Matrix(R, R) {
for (int i = 0; i < R; i++) v[i][i] = 0;
}
Matrix(int R, int C): r(R), c(C) {
v.assign(R, vector<int>(C, inf));
}
Matrix operator * (const Matrix &M){
Matrix res(r, M.c);
for (int i = 0; i < r; i++){
for (int k = 0; k < c; k++){
for (int j = 0; j < M.c; j++){
res.v[i][j] = min(res.v[i][j], v[i][k] + M.v[k][j]);
}
}
}
return res;
}
Matrix operator ^ (int a){
Matrix res(r);
for (Matrix exp = *this; a; a >>= 1, exp = exp * exp){
if (a & 1) res = res * exp;
}
return res;
}
};
void main_program(){
int n, m; cin >> n >> m;
Matrix mult(n);
for (int i = 0; i < m; i++){
int a, b, w; cin >> a >> b >> w;
a--; b--;
mult.v[a][b] = min(mult.v[a][b], w);
}
int k, q; cin >> k >> q;
mult = mult ^ k;
for (int qr = 0; qr < q; qr++){
int c, d; cin >> c >> d;
c--; d--;
Matrix base(1, n); base.v[0][c] = 0;
Matrix res = base * mult;
if (res.v[0][d] == inf) cout << "-1\n";
else cout << res.v[0][d] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
492 KB |
Output is correct |
2 |
Correct |
29 ms |
492 KB |
Output is correct |
3 |
Correct |
29 ms |
496 KB |
Output is correct |
4 |
Correct |
36 ms |
468 KB |
Output is correct |
5 |
Correct |
29 ms |
492 KB |
Output is correct |
6 |
Correct |
30 ms |
484 KB |
Output is correct |
7 |
Correct |
30 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
224 KB |
Output is correct |
7 |
Correct |
32 ms |
468 KB |
Output is correct |
8 |
Correct |
34 ms |
512 KB |
Output is correct |
9 |
Correct |
32 ms |
484 KB |
Output is correct |
10 |
Correct |
32 ms |
468 KB |
Output is correct |
11 |
Correct |
30 ms |
488 KB |
Output is correct |
12 |
Correct |
32 ms |
480 KB |
Output is correct |
13 |
Correct |
114 ms |
464 KB |
Output is correct |
14 |
Correct |
109 ms |
352 KB |
Output is correct |
15 |
Correct |
116 ms |
460 KB |
Output is correct |
16 |
Correct |
109 ms |
356 KB |
Output is correct |
17 |
Correct |
110 ms |
468 KB |
Output is correct |
18 |
Correct |
108 ms |
480 KB |
Output is correct |
19 |
Correct |
118 ms |
592 KB |
Output is correct |
20 |
Correct |
116 ms |
472 KB |
Output is correct |
21 |
Correct |
112 ms |
468 KB |
Output is correct |
22 |
Correct |
113 ms |
480 KB |
Output is correct |
23 |
Correct |
116 ms |
460 KB |
Output is correct |
24 |
Correct |
112 ms |
468 KB |
Output is correct |
25 |
Correct |
122 ms |
464 KB |
Output is correct |
26 |
Correct |
114 ms |
484 KB |
Output is correct |
27 |
Correct |
116 ms |
464 KB |
Output is correct |
28 |
Correct |
112 ms |
472 KB |
Output is correct |
29 |
Correct |
118 ms |
464 KB |
Output is correct |
30 |
Correct |
151 ms |
460 KB |
Output is correct |
31 |
Correct |
135 ms |
468 KB |
Output is correct |
32 |
Correct |
110 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
224 KB |
Output is correct |
7 |
Correct |
28 ms |
492 KB |
Output is correct |
8 |
Correct |
29 ms |
492 KB |
Output is correct |
9 |
Correct |
29 ms |
496 KB |
Output is correct |
10 |
Correct |
36 ms |
468 KB |
Output is correct |
11 |
Correct |
29 ms |
492 KB |
Output is correct |
12 |
Correct |
30 ms |
484 KB |
Output is correct |
13 |
Correct |
30 ms |
488 KB |
Output is correct |
14 |
Correct |
32 ms |
468 KB |
Output is correct |
15 |
Correct |
34 ms |
512 KB |
Output is correct |
16 |
Correct |
32 ms |
484 KB |
Output is correct |
17 |
Correct |
32 ms |
468 KB |
Output is correct |
18 |
Correct |
30 ms |
488 KB |
Output is correct |
19 |
Correct |
32 ms |
480 KB |
Output is correct |
20 |
Correct |
114 ms |
464 KB |
Output is correct |
21 |
Correct |
109 ms |
352 KB |
Output is correct |
22 |
Correct |
116 ms |
460 KB |
Output is correct |
23 |
Correct |
109 ms |
356 KB |
Output is correct |
24 |
Correct |
110 ms |
468 KB |
Output is correct |
25 |
Correct |
108 ms |
480 KB |
Output is correct |
26 |
Correct |
118 ms |
592 KB |
Output is correct |
27 |
Correct |
116 ms |
472 KB |
Output is correct |
28 |
Correct |
112 ms |
468 KB |
Output is correct |
29 |
Correct |
113 ms |
480 KB |
Output is correct |
30 |
Correct |
116 ms |
460 KB |
Output is correct |
31 |
Correct |
112 ms |
468 KB |
Output is correct |
32 |
Correct |
122 ms |
464 KB |
Output is correct |
33 |
Correct |
114 ms |
484 KB |
Output is correct |
34 |
Correct |
116 ms |
464 KB |
Output is correct |
35 |
Correct |
112 ms |
472 KB |
Output is correct |
36 |
Correct |
118 ms |
464 KB |
Output is correct |
37 |
Correct |
151 ms |
460 KB |
Output is correct |
38 |
Correct |
135 ms |
468 KB |
Output is correct |
39 |
Correct |
110 ms |
468 KB |
Output is correct |
40 |
Correct |
29 ms |
520 KB |
Output is correct |
41 |
Correct |
42 ms |
596 KB |
Output is correct |
42 |
Correct |
30 ms |
480 KB |
Output is correct |
43 |
Correct |
45 ms |
484 KB |
Output is correct |
44 |
Correct |
30 ms |
488 KB |
Output is correct |
45 |
Correct |
47 ms |
476 KB |
Output is correct |
46 |
Correct |
109 ms |
472 KB |
Output is correct |
47 |
Correct |
109 ms |
464 KB |
Output is correct |
48 |
Correct |
112 ms |
468 KB |
Output is correct |
49 |
Correct |
123 ms |
476 KB |
Output is correct |
50 |
Correct |
114 ms |
480 KB |
Output is correct |
51 |
Correct |
114 ms |
468 KB |
Output is correct |
52 |
Correct |
114 ms |
464 KB |
Output is correct |
53 |
Correct |
122 ms |
468 KB |
Output is correct |
54 |
Correct |
111 ms |
468 KB |
Output is correct |
55 |
Correct |
118 ms |
588 KB |
Output is correct |
56 |
Correct |
115 ms |
476 KB |
Output is correct |
57 |
Correct |
123 ms |
464 KB |
Output is correct |
58 |
Correct |
111 ms |
468 KB |
Output is correct |
59 |
Correct |
135 ms |
472 KB |
Output is correct |
60 |
Correct |
114 ms |
480 KB |
Output is correct |
61 |
Correct |
126 ms |
464 KB |
Output is correct |
62 |
Correct |
108 ms |
476 KB |
Output is correct |
63 |
Correct |
114 ms |
468 KB |
Output is correct |
64 |
Correct |
116 ms |
592 KB |
Output is correct |
65 |
Correct |
121 ms |
492 KB |
Output is correct |