제출 #1092705

#제출 시각아이디문제언어결과실행 시간메모리
1092705ortsacRailway Trip 2 (JOI22_ho_t4)C++17
0 / 100
2098 ms524288 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; vector<int> adj[MAXN]; int32_t main() { int n, x, m; cin >> n >> x >> m; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; if (a < b) { for (int j = a; j <= min(a + x - 1, b); j++) { for (int k = j; k <= b; k++) adj[j].push_back(k); } } else { for (int j = a; j >= max(a - x + 1, b); j--) { for (int k = j; k >= b; k--) adj[j].push_back(k); } } } int q; cin >> q; while (q--) { int s, t; cin >> s >> t; vector<bool> vis(n + 1); vector<int> d(n + 1); vis[s] = 1; queue<int> q; q.push(s); while (!q.empty()) { auto u = q.front(); q.pop(); for (auto v : adj[u]) { if (vis[v]) continue; vis[v] = 1; d[v] = (d[u] + 1); q.push(v); } } if (!vis[t]) cout << "-1\n"; else cout << d[t] << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...