#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define inf ((int)1e9)
using namespace std;
const int N = 71;
vector <pair<int, int> > adj[N];
vector <vector<vector<int> > > ans(N);
void dijkstra(int st, int K) {
// cout << st << ":\n";
vector <vector<int> > dist(N, vector<int>(N, inf));
vector <vector<bool> > vis(N, vector<bool>(N));
dist[st][0] = 0;
priority_queue <array<int, 3>, vector<array<int,3> >, greater<array<int, 3>> > pq; // {k, -v, node}
pq.push({0, 0, st});
while(!pq.empty()) {
auto [k, v, node] = pq.top();
pq.pop();
if(k > K or vis[node][k]) continue;
// cout << node << " " << dist[node][k] << "\n";
vis[node][k] = 1;
for(auto [to, w]:adj[node]) {
if(dist[to][k + 1] > v + w) {
pq.push({k + 1, v + w, to});
dist[to][k + 1] = v + w;
}
}
}
ans[st] = dist;
};
int32_t main(){
fast
int n, m;
cin >> n >> m;
vector <vector<int> > temp(n+1, vector<int>(n + 1, inf));
for(int i = 0; i < m; i++) {
int a, b, w;
cin >> a >> b >> w;
temp[a][b] = min(temp[a][b], w);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == j) temp[i][j] = 0;
if(temp[i][j] == inf) continue;
adj[i].push_back({j, temp[i][j]});
}
}
int K, Q;
cin >> K >> Q;
K = min(K, n - 1);
for(int i = 1; i <= n; i++) {
dijkstra(i, K);
}
while(Q--) {
int a, b;
cin >> a >> b;
cout << (ans[a][b][K] == inf ? -1 : ans[a][b][K]) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2140 KB |
Output is correct |
3 |
Correct |
4 ms |
1996 KB |
Output is correct |
4 |
Correct |
4 ms |
2140 KB |
Output is correct |
5 |
Correct |
6 ms |
2140 KB |
Output is correct |
6 |
Correct |
7 ms |
2140 KB |
Output is correct |
7 |
Correct |
8 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
17 ms |
2140 KB |
Output is correct |
8 |
Correct |
42 ms |
2108 KB |
Output is correct |
9 |
Correct |
6 ms |
2140 KB |
Output is correct |
10 |
Correct |
36 ms |
2056 KB |
Output is correct |
11 |
Correct |
22 ms |
2140 KB |
Output is correct |
12 |
Correct |
77 ms |
2220 KB |
Output is correct |
13 |
Correct |
71 ms |
8252 KB |
Output is correct |
14 |
Correct |
94 ms |
8128 KB |
Output is correct |
15 |
Correct |
166 ms |
8260 KB |
Output is correct |
16 |
Correct |
102 ms |
8276 KB |
Output is correct |
17 |
Correct |
80 ms |
8380 KB |
Output is correct |
18 |
Correct |
67 ms |
8400 KB |
Output is correct |
19 |
Correct |
164 ms |
8384 KB |
Output is correct |
20 |
Correct |
121 ms |
8272 KB |
Output is correct |
21 |
Correct |
71 ms |
8528 KB |
Output is correct |
22 |
Correct |
79 ms |
8400 KB |
Output is correct |
23 |
Correct |
78 ms |
8280 KB |
Output is correct |
24 |
Correct |
107 ms |
8272 KB |
Output is correct |
25 |
Correct |
70 ms |
8532 KB |
Output is correct |
26 |
Correct |
93 ms |
8404 KB |
Output is correct |
27 |
Correct |
104 ms |
8532 KB |
Output is correct |
28 |
Correct |
97 ms |
8400 KB |
Output is correct |
29 |
Correct |
72 ms |
8272 KB |
Output is correct |
30 |
Correct |
95 ms |
8272 KB |
Output is correct |
31 |
Correct |
136 ms |
8272 KB |
Output is correct |
32 |
Correct |
202 ms |
8132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
2140 KB |
Output is correct |
8 |
Correct |
3 ms |
2140 KB |
Output is correct |
9 |
Correct |
4 ms |
1996 KB |
Output is correct |
10 |
Correct |
4 ms |
2140 KB |
Output is correct |
11 |
Correct |
6 ms |
2140 KB |
Output is correct |
12 |
Correct |
7 ms |
2140 KB |
Output is correct |
13 |
Correct |
8 ms |
2140 KB |
Output is correct |
14 |
Correct |
17 ms |
2140 KB |
Output is correct |
15 |
Correct |
42 ms |
2108 KB |
Output is correct |
16 |
Correct |
6 ms |
2140 KB |
Output is correct |
17 |
Correct |
36 ms |
2056 KB |
Output is correct |
18 |
Correct |
22 ms |
2140 KB |
Output is correct |
19 |
Correct |
77 ms |
2220 KB |
Output is correct |
20 |
Correct |
71 ms |
8252 KB |
Output is correct |
21 |
Correct |
94 ms |
8128 KB |
Output is correct |
22 |
Correct |
166 ms |
8260 KB |
Output is correct |
23 |
Correct |
102 ms |
8276 KB |
Output is correct |
24 |
Correct |
80 ms |
8380 KB |
Output is correct |
25 |
Correct |
67 ms |
8400 KB |
Output is correct |
26 |
Correct |
164 ms |
8384 KB |
Output is correct |
27 |
Correct |
121 ms |
8272 KB |
Output is correct |
28 |
Correct |
71 ms |
8528 KB |
Output is correct |
29 |
Correct |
79 ms |
8400 KB |
Output is correct |
30 |
Correct |
78 ms |
8280 KB |
Output is correct |
31 |
Correct |
107 ms |
8272 KB |
Output is correct |
32 |
Correct |
70 ms |
8532 KB |
Output is correct |
33 |
Correct |
93 ms |
8404 KB |
Output is correct |
34 |
Correct |
104 ms |
8532 KB |
Output is correct |
35 |
Correct |
97 ms |
8400 KB |
Output is correct |
36 |
Correct |
72 ms |
8272 KB |
Output is correct |
37 |
Correct |
95 ms |
8272 KB |
Output is correct |
38 |
Correct |
136 ms |
8272 KB |
Output is correct |
39 |
Correct |
202 ms |
8132 KB |
Output is correct |
40 |
Correct |
13 ms |
2136 KB |
Output is correct |
41 |
Correct |
42 ms |
2132 KB |
Output is correct |
42 |
Correct |
10 ms |
2140 KB |
Output is correct |
43 |
Correct |
67 ms |
2132 KB |
Output is correct |
44 |
Correct |
20 ms |
2132 KB |
Output is correct |
45 |
Correct |
115 ms |
2132 KB |
Output is correct |
46 |
Correct |
72 ms |
8280 KB |
Output is correct |
47 |
Correct |
128 ms |
8136 KB |
Output is correct |
48 |
Correct |
91 ms |
8272 KB |
Output is correct |
49 |
Correct |
188 ms |
8256 KB |
Output is correct |
50 |
Correct |
70 ms |
8264 KB |
Output is correct |
51 |
Correct |
94 ms |
8528 KB |
Output is correct |
52 |
Correct |
80 ms |
8276 KB |
Output is correct |
53 |
Correct |
164 ms |
8356 KB |
Output is correct |
54 |
Correct |
67 ms |
8400 KB |
Output is correct |
55 |
Correct |
74 ms |
8272 KB |
Output is correct |
56 |
Correct |
152 ms |
8276 KB |
Output is correct |
57 |
Correct |
146 ms |
8276 KB |
Output is correct |
58 |
Correct |
68 ms |
8532 KB |
Output is correct |
59 |
Correct |
113 ms |
8528 KB |
Output is correct |
60 |
Correct |
195 ms |
8524 KB |
Output is correct |
61 |
Correct |
180 ms |
8532 KB |
Output is correct |
62 |
Correct |
67 ms |
8260 KB |
Output is correct |
63 |
Correct |
98 ms |
8260 KB |
Output is correct |
64 |
Correct |
108 ms |
8276 KB |
Output is correct |
65 |
Correct |
206 ms |
8276 KB |
Output is correct |