#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);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
25 ms |
1652 KB |
Output is correct |
8 |
Correct |
79 ms |
3468 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
67 ms |
2252 KB |
Output is correct |
11 |
Correct |
27 ms |
980 KB |
Output is correct |
12 |
Correct |
130 ms |
2732 KB |
Output is correct |
13 |
Correct |
78 ms |
828 KB |
Output is correct |
14 |
Correct |
84 ms |
864 KB |
Output is correct |
15 |
Correct |
273 ms |
3276 KB |
Output is correct |
16 |
Correct |
137 ms |
7732 KB |
Output is correct |
17 |
Correct |
83 ms |
6964 KB |
Output is correct |
18 |
Correct |
75 ms |
6976 KB |
Output is correct |
19 |
Correct |
259 ms |
9912 KB |
Output is correct |
20 |
Correct |
165 ms |
8468 KB |
Output is correct |
21 |
Correct |
75 ms |
6976 KB |
Output is correct |
22 |
Correct |
104 ms |
7500 KB |
Output is correct |
23 |
Correct |
110 ms |
7352 KB |
Output is correct |
24 |
Correct |
168 ms |
8380 KB |
Output is correct |
25 |
Correct |
78 ms |
7108 KB |
Output is correct |
26 |
Correct |
120 ms |
7608 KB |
Output is correct |
27 |
Correct |
149 ms |
8036 KB |
Output is correct |
28 |
Correct |
128 ms |
7788 KB |
Output is correct |
29 |
Correct |
80 ms |
6968 KB |
Output is correct |
30 |
Correct |
113 ms |
7480 KB |
Output is correct |
31 |
Correct |
193 ms |
8708 KB |
Output is correct |
32 |
Correct |
344 ms |
10048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
6 ms |
804 KB |
Output is correct |
14 |
Correct |
25 ms |
1652 KB |
Output is correct |
15 |
Correct |
79 ms |
3468 KB |
Output is correct |
16 |
Correct |
5 ms |
724 KB |
Output is correct |
17 |
Correct |
67 ms |
2252 KB |
Output is correct |
18 |
Correct |
27 ms |
980 KB |
Output is correct |
19 |
Correct |
130 ms |
2732 KB |
Output is correct |
20 |
Correct |
78 ms |
828 KB |
Output is correct |
21 |
Correct |
84 ms |
864 KB |
Output is correct |
22 |
Correct |
273 ms |
3276 KB |
Output is correct |
23 |
Correct |
137 ms |
7732 KB |
Output is correct |
24 |
Correct |
83 ms |
6964 KB |
Output is correct |
25 |
Correct |
75 ms |
6976 KB |
Output is correct |
26 |
Correct |
259 ms |
9912 KB |
Output is correct |
27 |
Correct |
165 ms |
8468 KB |
Output is correct |
28 |
Correct |
75 ms |
6976 KB |
Output is correct |
29 |
Correct |
104 ms |
7500 KB |
Output is correct |
30 |
Correct |
110 ms |
7352 KB |
Output is correct |
31 |
Correct |
168 ms |
8380 KB |
Output is correct |
32 |
Correct |
78 ms |
7108 KB |
Output is correct |
33 |
Correct |
120 ms |
7608 KB |
Output is correct |
34 |
Correct |
149 ms |
8036 KB |
Output is correct |
35 |
Correct |
128 ms |
7788 KB |
Output is correct |
36 |
Correct |
80 ms |
6968 KB |
Output is correct |
37 |
Correct |
113 ms |
7480 KB |
Output is correct |
38 |
Correct |
193 ms |
8708 KB |
Output is correct |
39 |
Correct |
344 ms |
10048 KB |
Output is correct |
40 |
Correct |
18 ms |
1420 KB |
Output is correct |
41 |
Correct |
74 ms |
3476 KB |
Output is correct |
42 |
Correct |
16 ms |
992 KB |
Output is correct |
43 |
Correct |
146 ms |
3504 KB |
Output is correct |
44 |
Correct |
26 ms |
1180 KB |
Output is correct |
45 |
Correct |
224 ms |
4012 KB |
Output is correct |
46 |
Correct |
89 ms |
6988 KB |
Output is correct |
47 |
Correct |
189 ms |
8376 KB |
Output is correct |
48 |
Correct |
116 ms |
7480 KB |
Output is correct |
49 |
Correct |
316 ms |
10008 KB |
Output is correct |
50 |
Correct |
80 ms |
6968 KB |
Output is correct |
51 |
Correct |
120 ms |
7656 KB |
Output is correct |
52 |
Correct |
76 ms |
6960 KB |
Output is correct |
53 |
Correct |
264 ms |
9816 KB |
Output is correct |
54 |
Correct |
81 ms |
7084 KB |
Output is correct |
55 |
Correct |
85 ms |
7116 KB |
Output is correct |
56 |
Correct |
248 ms |
9480 KB |
Output is correct |
57 |
Correct |
268 ms |
9720 KB |
Output is correct |
58 |
Correct |
76 ms |
6988 KB |
Output is correct |
59 |
Correct |
170 ms |
8268 KB |
Output is correct |
60 |
Correct |
262 ms |
9368 KB |
Output is correct |
61 |
Correct |
323 ms |
9912 KB |
Output is correct |
62 |
Correct |
85 ms |
6844 KB |
Output is correct |
63 |
Correct |
122 ms |
7612 KB |
Output is correct |
64 |
Correct |
139 ms |
7944 KB |
Output is correct |
65 |
Correct |
323 ms |
10072 KB |
Output is correct |