이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//{ <defines>
#ifndef _LOCAL
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>
using namespace std;
#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)
#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _ << ' ' <<
#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back
#define sz(x) int(x.size())
#define pw(x) (1 << (x))
#define PW(x) (1ll << (x))
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset(x, y, sizeof x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;
const int oo = 2e9;
const ll OO = 4e18;
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int md = 0x3b800001;
const int MD = 1e9 + 7;
inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());
template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
//} </defines>
const int N = 3e4 + 5;
int n, m, st, fn, d[N];
vi g[N];
bool mk[N];
void solve() {
cin >> n >> m;
fo(m) {
int b, p;
cin >> b >> p;
if(i == 0) st = b;
if(i == 1) fn = b;
g[b].pb(p);
}
fo(n) d[i] = N;
d[st] = 0;
fo(n) sort(bnd(g[i]));
struct sc {
inline bool operator() (const int &a, const int &b) const
{re d[a] == d[b] ? a < b : d[a] < d[b];}
};
set<int, sc> f;
f.insert(st);
for(; !f.empty(); ) {
int v = *f.begin();
f.erase(f.begin());
mk[v] = 1;
if(v == fn) re void(cout << d[fn] << endl);
int pr = -1;
for(int p : g[v]) if(p != pr) {
pr = p;
for(int sign = -1; sign <= 1; sign += 2) {
for(int w = 1, u = v + sign * p; u >= 0 && u < n; ++w, u += sign * p) {
if(mk[u]) continue;
int dd = d[v] + w;
if(dd < d[u]) {
auto c = f.find(u);
if(c != f.end()) f.erase(c);
d[u] = dd;
f.insert(u);
}
}
}
}
}
cout << -1 << endl;
}
/*
5 3
0 2
1 1
4 1
*/
int main() {
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |