#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 |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
1 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 |
2392 KB |
Output is correct |
3 |
Correct |
3 ms |
2180 KB |
Output is correct |
4 |
Correct |
4 ms |
2140 KB |
Output is correct |
5 |
Correct |
5 ms |
2112 KB |
Output is correct |
6 |
Correct |
6 ms |
2136 KB |
Output is correct |
7 |
Correct |
10 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
1 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 |
1960 KB |
Output is correct |
8 |
Correct |
43 ms |
2056 KB |
Output is correct |
9 |
Correct |
6 ms |
2136 KB |
Output is correct |
10 |
Correct |
36 ms |
2116 KB |
Output is correct |
11 |
Correct |
19 ms |
2140 KB |
Output is correct |
12 |
Correct |
78 ms |
2096 KB |
Output is correct |
13 |
Correct |
72 ms |
8272 KB |
Output is correct |
14 |
Correct |
75 ms |
8272 KB |
Output is correct |
15 |
Correct |
177 ms |
8272 KB |
Output is correct |
16 |
Correct |
107 ms |
8128 KB |
Output is correct |
17 |
Correct |
73 ms |
8272 KB |
Output is correct |
18 |
Correct |
68 ms |
8276 KB |
Output is correct |
19 |
Correct |
169 ms |
8528 KB |
Output is correct |
20 |
Correct |
121 ms |
8396 KB |
Output is correct |
21 |
Correct |
77 ms |
8276 KB |
Output is correct |
22 |
Correct |
87 ms |
8272 KB |
Output is correct |
23 |
Correct |
81 ms |
8276 KB |
Output is correct |
24 |
Correct |
109 ms |
8272 KB |
Output is correct |
25 |
Correct |
72 ms |
8528 KB |
Output is correct |
26 |
Correct |
100 ms |
8652 KB |
Output is correct |
27 |
Correct |
112 ms |
8532 KB |
Output is correct |
28 |
Correct |
101 ms |
8528 KB |
Output is correct |
29 |
Correct |
71 ms |
8528 KB |
Output is correct |
30 |
Correct |
92 ms |
8276 KB |
Output is correct |
31 |
Correct |
139 ms |
8272 KB |
Output is correct |
32 |
Correct |
210 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
1 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 |
2392 KB |
Output is correct |
9 |
Correct |
3 ms |
2180 KB |
Output is correct |
10 |
Correct |
4 ms |
2140 KB |
Output is correct |
11 |
Correct |
5 ms |
2112 KB |
Output is correct |
12 |
Correct |
6 ms |
2136 KB |
Output is correct |
13 |
Correct |
10 ms |
2140 KB |
Output is correct |
14 |
Correct |
17 ms |
1960 KB |
Output is correct |
15 |
Correct |
43 ms |
2056 KB |
Output is correct |
16 |
Correct |
6 ms |
2136 KB |
Output is correct |
17 |
Correct |
36 ms |
2116 KB |
Output is correct |
18 |
Correct |
19 ms |
2140 KB |
Output is correct |
19 |
Correct |
78 ms |
2096 KB |
Output is correct |
20 |
Correct |
72 ms |
8272 KB |
Output is correct |
21 |
Correct |
75 ms |
8272 KB |
Output is correct |
22 |
Correct |
177 ms |
8272 KB |
Output is correct |
23 |
Correct |
107 ms |
8128 KB |
Output is correct |
24 |
Correct |
73 ms |
8272 KB |
Output is correct |
25 |
Correct |
68 ms |
8276 KB |
Output is correct |
26 |
Correct |
169 ms |
8528 KB |
Output is correct |
27 |
Correct |
121 ms |
8396 KB |
Output is correct |
28 |
Correct |
77 ms |
8276 KB |
Output is correct |
29 |
Correct |
87 ms |
8272 KB |
Output is correct |
30 |
Correct |
81 ms |
8276 KB |
Output is correct |
31 |
Correct |
109 ms |
8272 KB |
Output is correct |
32 |
Correct |
72 ms |
8528 KB |
Output is correct |
33 |
Correct |
100 ms |
8652 KB |
Output is correct |
34 |
Correct |
112 ms |
8532 KB |
Output is correct |
35 |
Correct |
101 ms |
8528 KB |
Output is correct |
36 |
Correct |
71 ms |
8528 KB |
Output is correct |
37 |
Correct |
92 ms |
8276 KB |
Output is correct |
38 |
Correct |
139 ms |
8272 KB |
Output is correct |
39 |
Correct |
210 ms |
8280 KB |
Output is correct |
40 |
Correct |
14 ms |
2184 KB |
Output is correct |
41 |
Correct |
42 ms |
2104 KB |
Output is correct |
42 |
Correct |
11 ms |
2140 KB |
Output is correct |
43 |
Correct |
67 ms |
1940 KB |
Output is correct |
44 |
Correct |
20 ms |
2140 KB |
Output is correct |
45 |
Correct |
115 ms |
2228 KB |
Output is correct |
46 |
Correct |
73 ms |
8276 KB |
Output is correct |
47 |
Correct |
125 ms |
8140 KB |
Output is correct |
48 |
Correct |
108 ms |
8124 KB |
Output is correct |
49 |
Correct |
188 ms |
8128 KB |
Output is correct |
50 |
Correct |
76 ms |
8528 KB |
Output is correct |
51 |
Correct |
93 ms |
8276 KB |
Output is correct |
52 |
Correct |
71 ms |
8396 KB |
Output is correct |
53 |
Correct |
163 ms |
8276 KB |
Output is correct |
54 |
Correct |
72 ms |
8272 KB |
Output is correct |
55 |
Correct |
75 ms |
8448 KB |
Output is correct |
56 |
Correct |
174 ms |
8272 KB |
Output is correct |
57 |
Correct |
152 ms |
8404 KB |
Output is correct |
58 |
Correct |
80 ms |
8524 KB |
Output is correct |
59 |
Correct |
121 ms |
8544 KB |
Output is correct |
60 |
Correct |
158 ms |
8528 KB |
Output is correct |
61 |
Correct |
181 ms |
8396 KB |
Output is correct |
62 |
Correct |
70 ms |
8276 KB |
Output is correct |
63 |
Correct |
103 ms |
8124 KB |
Output is correct |
64 |
Correct |
111 ms |
8180 KB |
Output is correct |
65 |
Correct |
208 ms |
8276 KB |
Output is correct |