#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int INF=1e9;
int n, m, k, q, d[70][70], dp[70][70][70];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j)
d[i][j]=i==j?0:INF;
for (int i=0; i<m; ++i) {
int u, v, t;
cin >> u >> v >> t, --u, --v;
d[u][v]=min(d[u][v], t);
}
cin >> k >> q;
k=min(k, n-1);
memset(dp, 0x3f, sizeof(dp));
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) {
dp[0][i][j]=i==j?0:INF;
dp[1][i][j]=d[i][j];
}
for (int i=2; i<=k; ++i)
for (int a=0; a<n; ++a)
for (int b=0; b<n; ++b)
for (int c=0; c<n; ++c)
dp[i][a][c]=min(dp[i][a][c], dp[i-1][a][b]+d[b][c]);
while(q--) {
int a, b;
cin >> a >> b, --a, --b;
cout << (dp[k][a][b]>=INF?-1:dp[k][a][b]) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1612 KB |
Output is correct |
3 |
Correct |
2 ms |
1696 KB |
Output is correct |
4 |
Correct |
3 ms |
1612 KB |
Output is correct |
5 |
Correct |
3 ms |
1612 KB |
Output is correct |
6 |
Correct |
3 ms |
1612 KB |
Output is correct |
7 |
Correct |
4 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
11 ms |
1676 KB |
Output is correct |
8 |
Correct |
25 ms |
1672 KB |
Output is correct |
9 |
Correct |
3 ms |
1676 KB |
Output is correct |
10 |
Correct |
14 ms |
1680 KB |
Output is correct |
11 |
Correct |
7 ms |
1668 KB |
Output is correct |
12 |
Correct |
17 ms |
1676 KB |
Output is correct |
13 |
Correct |
85 ms |
1600 KB |
Output is correct |
14 |
Correct |
84 ms |
1608 KB |
Output is correct |
15 |
Correct |
100 ms |
1652 KB |
Output is correct |
16 |
Correct |
102 ms |
1604 KB |
Output is correct |
17 |
Correct |
92 ms |
1604 KB |
Output is correct |
18 |
Correct |
84 ms |
1600 KB |
Output is correct |
19 |
Correct |
108 ms |
1600 KB |
Output is correct |
20 |
Correct |
96 ms |
1656 KB |
Output is correct |
21 |
Correct |
87 ms |
1616 KB |
Output is correct |
22 |
Correct |
86 ms |
1608 KB |
Output is correct |
23 |
Correct |
97 ms |
1732 KB |
Output is correct |
24 |
Correct |
101 ms |
1596 KB |
Output is correct |
25 |
Correct |
93 ms |
1616 KB |
Output is correct |
26 |
Correct |
89 ms |
1604 KB |
Output is correct |
27 |
Correct |
104 ms |
1728 KB |
Output is correct |
28 |
Correct |
90 ms |
1600 KB |
Output is correct |
29 |
Correct |
85 ms |
1608 KB |
Output is correct |
30 |
Correct |
87 ms |
1604 KB |
Output is correct |
31 |
Correct |
96 ms |
1600 KB |
Output is correct |
32 |
Correct |
118 ms |
1732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
8 |
Correct |
2 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1696 KB |
Output is correct |
10 |
Correct |
3 ms |
1612 KB |
Output is correct |
11 |
Correct |
3 ms |
1612 KB |
Output is correct |
12 |
Correct |
3 ms |
1612 KB |
Output is correct |
13 |
Correct |
4 ms |
1612 KB |
Output is correct |
14 |
Correct |
11 ms |
1676 KB |
Output is correct |
15 |
Correct |
25 ms |
1672 KB |
Output is correct |
16 |
Correct |
3 ms |
1676 KB |
Output is correct |
17 |
Correct |
14 ms |
1680 KB |
Output is correct |
18 |
Correct |
7 ms |
1668 KB |
Output is correct |
19 |
Correct |
17 ms |
1676 KB |
Output is correct |
20 |
Correct |
85 ms |
1600 KB |
Output is correct |
21 |
Correct |
84 ms |
1608 KB |
Output is correct |
22 |
Correct |
100 ms |
1652 KB |
Output is correct |
23 |
Correct |
102 ms |
1604 KB |
Output is correct |
24 |
Correct |
92 ms |
1604 KB |
Output is correct |
25 |
Correct |
84 ms |
1600 KB |
Output is correct |
26 |
Correct |
108 ms |
1600 KB |
Output is correct |
27 |
Correct |
96 ms |
1656 KB |
Output is correct |
28 |
Correct |
87 ms |
1616 KB |
Output is correct |
29 |
Correct |
86 ms |
1608 KB |
Output is correct |
30 |
Correct |
97 ms |
1732 KB |
Output is correct |
31 |
Correct |
101 ms |
1596 KB |
Output is correct |
32 |
Correct |
93 ms |
1616 KB |
Output is correct |
33 |
Correct |
89 ms |
1604 KB |
Output is correct |
34 |
Correct |
104 ms |
1728 KB |
Output is correct |
35 |
Correct |
90 ms |
1600 KB |
Output is correct |
36 |
Correct |
85 ms |
1608 KB |
Output is correct |
37 |
Correct |
87 ms |
1604 KB |
Output is correct |
38 |
Correct |
96 ms |
1600 KB |
Output is correct |
39 |
Correct |
118 ms |
1732 KB |
Output is correct |
40 |
Correct |
8 ms |
1592 KB |
Output is correct |
41 |
Correct |
24 ms |
1672 KB |
Output is correct |
42 |
Correct |
4 ms |
1612 KB |
Output is correct |
43 |
Correct |
24 ms |
1680 KB |
Output is correct |
44 |
Correct |
5 ms |
1612 KB |
Output is correct |
45 |
Correct |
25 ms |
1596 KB |
Output is correct |
46 |
Correct |
111 ms |
1600 KB |
Output is correct |
47 |
Correct |
91 ms |
1584 KB |
Output is correct |
48 |
Correct |
90 ms |
1608 KB |
Output is correct |
49 |
Correct |
111 ms |
1612 KB |
Output is correct |
50 |
Correct |
85 ms |
1612 KB |
Output is correct |
51 |
Correct |
91 ms |
1600 KB |
Output is correct |
52 |
Correct |
83 ms |
1600 KB |
Output is correct |
53 |
Correct |
135 ms |
1608 KB |
Output is correct |
54 |
Correct |
85 ms |
1616 KB |
Output is correct |
55 |
Correct |
85 ms |
1600 KB |
Output is correct |
56 |
Correct |
103 ms |
1600 KB |
Output is correct |
57 |
Correct |
110 ms |
1596 KB |
Output is correct |
58 |
Correct |
87 ms |
1604 KB |
Output is correct |
59 |
Correct |
121 ms |
1612 KB |
Output is correct |
60 |
Correct |
107 ms |
1596 KB |
Output is correct |
61 |
Correct |
112 ms |
1668 KB |
Output is correct |
62 |
Correct |
98 ms |
1608 KB |
Output is correct |
63 |
Correct |
86 ms |
1624 KB |
Output is correct |
64 |
Correct |
92 ms |
1600 KB |
Output is correct |
65 |
Correct |
109 ms |
1596 KB |
Output is correct |