# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
927151 | Amirreza_Fakhri | Jakarta Skyscrapers (APIO15_skyscraper) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 3e4+5;
int n, m, s, t, dist[maxn];
set <int> doge[maxn];
priority_queue<pii, vector<pii>, greater<pii> > pq;
void dij(int v) {
fill(dist, dist+n, inf);
dist[v] = 0;
pq.push(mp(0, v));
while (pq.size()) {
int d = pq.top().F, u = pq.top().S;
pq.pop();
if (dist[u] != d) continue;
dist[u] = d;
if (u == t) return cout << d << '\n', void();
for (int p : doge[u]) {
for (int j = 1; u+j*p < n; j++) {
st.insert(mp(d+j, u+j*p));
if (doge[u+j*p].find(p) != doge[u+j*p].end()) break;
}
for (int j = 1; u-j*p >= 0; j++) {
st.insert(mp(d+j, u-j*p));
if (doge[u-j*p].find(p) != doge[u-j*p].end()) break;
}
}
}
cout << -1 << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
doge[a].insert(b);
if (i == 0) s = a;
if (i == 1) t = a;
}
dij(s);
return 0;
}