#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Node = array<ll, 3>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
setIO();
int n, m;
cin >> n >> m;
vector<vector<pii> > graph(n+1);
int mat[n+1][n+1];
memset(mat, 0, sizeof(mat));
for(int i=0; i<m; i++) {
int a, b, w;
cin >> a >> b >> w;
if(mat[a][b] == 0) mat[a][b] = w;
else mat[a][b] = min(mat[a][b], w);
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(mat[i][j] != 0)
graph[i].pb({ j, mat[i][j] });
int k, q;
cin >> k >> q;
k = min(k, n);
vector<vector<ll> > ans(n+1, vector<ll>(n+1, 1e18));
vector<vector<vector<ll> > > dist(n+1, vector<vector<ll>>(n+1, vector<ll>(k+1, 1e18)));
priority_queue<Node, vector<Node>, greater<Node> > pq;
for(int i=1; i<=n; i++) {
dist[i][i][0] = 0;
bool vis[n+1][k+1];
memset(vis, 0, sizeof(vis));
pq.push({ 0, 0, i });
while(!pq.empty()) {
ll d = pq.top().at(0);
int c = pq.top().at(1);
int u = pq.top().at(2);
pq.pop();
if(vis[u][c]) continue;
vis[u][c] = true;
if(c == k) continue;
for(pii &v : graph[u]) {
if(dist[i][v.first][c+1] > dist[i][u][c] + v.second) {
dist[i][v.first][c+1] = dist[i][u][c] + v.second;
if(vis[v.first][c+1]) continue;
pq.push({ dist[i][v.first][c+1], c+1, v.first });
}
}
}
for(int j=1; j<=n; j++) {
for(int x=0; x<k+1; x++) {
ans[i][j] = min(ans[i][j], dist[i][j][x]);
}
}
}
while(q--) {
int a, b;
cin >> a >> b;
cout << (ans[a][b] == 1e18 ? -1 : ans[a][b]) << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:66:16: warning: unused variable 'd' [-Wunused-variable]
66 | ll d = pq.top().at(0);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
2 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
4 ms |
728 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
25 ms |
1628 KB |
Output is correct |
8 |
Correct |
79 ms |
3632 KB |
Output is correct |
9 |
Correct |
5 ms |
856 KB |
Output is correct |
10 |
Correct |
70 ms |
2140 KB |
Output is correct |
11 |
Correct |
28 ms |
1112 KB |
Output is correct |
12 |
Correct |
134 ms |
3116 KB |
Output is correct |
13 |
Correct |
79 ms |
7116 KB |
Output is correct |
14 |
Correct |
85 ms |
7248 KB |
Output is correct |
15 |
Correct |
285 ms |
9416 KB |
Output is correct |
16 |
Correct |
149 ms |
7960 KB |
Output is correct |
17 |
Correct |
80 ms |
7100 KB |
Output is correct |
18 |
Correct |
74 ms |
6848 KB |
Output is correct |
19 |
Correct |
247 ms |
10068 KB |
Output is correct |
20 |
Correct |
166 ms |
8376 KB |
Output is correct |
21 |
Correct |
74 ms |
6960 KB |
Output is correct |
22 |
Correct |
103 ms |
7624 KB |
Output is correct |
23 |
Correct |
105 ms |
7508 KB |
Output is correct |
24 |
Correct |
167 ms |
8504 KB |
Output is correct |
25 |
Correct |
78 ms |
7248 KB |
Output is correct |
26 |
Correct |
119 ms |
7620 KB |
Output is correct |
27 |
Correct |
146 ms |
8184 KB |
Output is correct |
28 |
Correct |
133 ms |
7864 KB |
Output is correct |
29 |
Correct |
80 ms |
6992 KB |
Output is correct |
30 |
Correct |
116 ms |
7480 KB |
Output is correct |
31 |
Correct |
196 ms |
8964 KB |
Output is correct |
32 |
Correct |
321 ms |
10064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
2 ms |
856 KB |
Output is correct |
9 |
Correct |
3 ms |
856 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
4 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
728 KB |
Output is correct |
13 |
Correct |
7 ms |
856 KB |
Output is correct |
14 |
Correct |
25 ms |
1628 KB |
Output is correct |
15 |
Correct |
79 ms |
3632 KB |
Output is correct |
16 |
Correct |
5 ms |
856 KB |
Output is correct |
17 |
Correct |
70 ms |
2140 KB |
Output is correct |
18 |
Correct |
28 ms |
1112 KB |
Output is correct |
19 |
Correct |
134 ms |
3116 KB |
Output is correct |
20 |
Correct |
79 ms |
7116 KB |
Output is correct |
21 |
Correct |
85 ms |
7248 KB |
Output is correct |
22 |
Correct |
285 ms |
9416 KB |
Output is correct |
23 |
Correct |
149 ms |
7960 KB |
Output is correct |
24 |
Correct |
80 ms |
7100 KB |
Output is correct |
25 |
Correct |
74 ms |
6848 KB |
Output is correct |
26 |
Correct |
247 ms |
10068 KB |
Output is correct |
27 |
Correct |
166 ms |
8376 KB |
Output is correct |
28 |
Correct |
74 ms |
6960 KB |
Output is correct |
29 |
Correct |
103 ms |
7624 KB |
Output is correct |
30 |
Correct |
105 ms |
7508 KB |
Output is correct |
31 |
Correct |
167 ms |
8504 KB |
Output is correct |
32 |
Correct |
78 ms |
7248 KB |
Output is correct |
33 |
Correct |
119 ms |
7620 KB |
Output is correct |
34 |
Correct |
146 ms |
8184 KB |
Output is correct |
35 |
Correct |
133 ms |
7864 KB |
Output is correct |
36 |
Correct |
80 ms |
6992 KB |
Output is correct |
37 |
Correct |
116 ms |
7480 KB |
Output is correct |
38 |
Correct |
196 ms |
8964 KB |
Output is correct |
39 |
Correct |
321 ms |
10064 KB |
Output is correct |
40 |
Correct |
18 ms |
1508 KB |
Output is correct |
41 |
Correct |
76 ms |
3420 KB |
Output is correct |
42 |
Correct |
13 ms |
1116 KB |
Output is correct |
43 |
Correct |
143 ms |
3420 KB |
Output is correct |
44 |
Correct |
26 ms |
1112 KB |
Output is correct |
45 |
Correct |
222 ms |
4144 KB |
Output is correct |
46 |
Correct |
81 ms |
6968 KB |
Output is correct |
47 |
Correct |
184 ms |
8724 KB |
Output is correct |
48 |
Correct |
115 ms |
7488 KB |
Output is correct |
49 |
Correct |
322 ms |
10128 KB |
Output is correct |
50 |
Correct |
79 ms |
6972 KB |
Output is correct |
51 |
Correct |
116 ms |
7764 KB |
Output is correct |
52 |
Correct |
80 ms |
6972 KB |
Output is correct |
53 |
Correct |
280 ms |
9916 KB |
Output is correct |
54 |
Correct |
81 ms |
7232 KB |
Output is correct |
55 |
Correct |
86 ms |
7100 KB |
Output is correct |
56 |
Correct |
248 ms |
9536 KB |
Output is correct |
57 |
Correct |
267 ms |
10008 KB |
Output is correct |
58 |
Correct |
77 ms |
6996 KB |
Output is correct |
59 |
Correct |
172 ms |
8376 KB |
Output is correct |
60 |
Correct |
260 ms |
9412 KB |
Output is correct |
61 |
Correct |
322 ms |
10068 KB |
Output is correct |
62 |
Correct |
77 ms |
6968 KB |
Output is correct |
63 |
Correct |
123 ms |
7612 KB |
Output is correct |
64 |
Correct |
163 ms |
8080 KB |
Output is correct |
65 |
Correct |
329 ms |
10344 KB |
Output is correct |