#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define int long long
#define inf ((int)1e18)
using namespace std;
const int N = 71;
vector <pair<int, int> > adj[N];
vector <vector<vector<int> > > ans(N);
int32_t main(){
fast
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back({b, w});
}
int K, Q;
cin >> K >> Q;
K = min(K, n - 1);
auto dijkstra = [&](int st) {
// 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 << " " << k << " " << v << "\n";
dist[node][k] = v;
vis[node][k] = 1;
for(auto [to, w]:adj[node]) {
if(dist[to][k + 1] == inf) {
pq.push({k + 1, v + w, to});
}
}
}
ans[st] = dist;
};
for(int i = 1; i <= n; i++) {
adj[i].push_back({i, 0});
}
for(int i = 1; i <= n; i++) {
dijkstra(i);
}
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 |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3420 KB |
Output is correct |
2 |
Correct |
5 ms |
3420 KB |
Output is correct |
3 |
Correct |
9 ms |
3356 KB |
Output is correct |
4 |
Correct |
16 ms |
3420 KB |
Output is correct |
5 |
Correct |
29 ms |
3656 KB |
Output is correct |
6 |
Correct |
101 ms |
4136 KB |
Output is correct |
7 |
Correct |
158 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
92 ms |
3528 KB |
Output is correct |
8 |
Correct |
258 ms |
3412 KB |
Output is correct |
9 |
Correct |
42 ms |
3644 KB |
Output is correct |
10 |
Correct |
415 ms |
3532 KB |
Output is correct |
11 |
Correct |
523 ms |
4148 KB |
Output is correct |
12 |
Execution timed out |
1012 ms |
2380 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
3420 KB |
Output is correct |
8 |
Correct |
5 ms |
3420 KB |
Output is correct |
9 |
Correct |
9 ms |
3356 KB |
Output is correct |
10 |
Correct |
16 ms |
3420 KB |
Output is correct |
11 |
Correct |
29 ms |
3656 KB |
Output is correct |
12 |
Correct |
101 ms |
4136 KB |
Output is correct |
13 |
Correct |
158 ms |
4188 KB |
Output is correct |
14 |
Correct |
92 ms |
3528 KB |
Output is correct |
15 |
Correct |
258 ms |
3412 KB |
Output is correct |
16 |
Correct |
42 ms |
3644 KB |
Output is correct |
17 |
Correct |
415 ms |
3532 KB |
Output is correct |
18 |
Correct |
523 ms |
4148 KB |
Output is correct |
19 |
Execution timed out |
1012 ms |
2380 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |