This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 30100
#define SQRT 180
#define BLK (MAXN / SQRT) + 10
int n, m;
int B[MAXN], p[MAXN];
vector<int> hab[MAXN];
bool mark[MAXN][SQRT];
int dist[MAXN][SQRT + 1];
vector<pair<pii, int>> adj[MAXN][SQRT + 1];
/*
inline void addedge(int x1, int y1, int x2, int y2, int w)
{
adj[x1][y1].pb({{x2, y2}, w});
}
*/
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> s;
inline void relax(int v, int p, int u, int up, int w)
{
if (dist[v][p] + w >= dist[u][up]) return;
dist[u][up] = dist[v][p] + w;
s.push({dist[u][up], {u, up}});
}
void dij(void)
{
FOR(i, 0, MAXN) fill(dist[i], dist[i] + SQRT + 1, INF);
dist[B[1]][0] = 0;
s.push({0, {B[1], 0}});
while (!s.empty())
{
auto pr = s.top();
s.pop();
int v = pr.sc.fr;
int pw = pr.sc.sc;
if (dist[v][pw] != pr.fr) continue;
// relax others
if (pw == SQRT) // people
{
for (int j = B[v]; j <= n; j += p[v])
{
relax(v, pw, j, 0, (j - B[v]) / p[v]);
}
for (int j = B[v] - p[v]; j >= 1; j -= p[v])
{
relax(v, pw, j, 0, (B[v] - j) / p[v]);
}
}
else // building
{
if (pw == 0)
{
FOR(j, 1, SQRT) if (mark[v][j])
relax(v, pw, v, j, 0);
for (int u : hab[v]) if (p[u] >= SQRT)
relax(v, pw, u, SQRT, 0);
}
else
{
relax(v, pw, v, 0, 0);
if (v + pw <= n) relax(v, pw, v + pw, pw, 1);
if (v - pw >= 1) relax(v, pw, v - pw, pw, 1);
}
}
}
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
FOR(i, 1, m + 1)
{
cin >> B[i] >> p[i];
B[i]++;
hab[B[i]].pb(i);
if (p[i] < SQRT) mark[B[i]][p[i]] = true;
}
/*
FOR(i, 1, n + 1)
{
FOR(j, 1, SQRT)
{
addedge(i, j, i, 0, 0);
if (mark[i][j]) addedge(i, 0, i, j, 0);
if (i + j <= n) addedge(i, j, i + j, j, 1);
if (i - j >= 1) addedge(i, j, i - j, j, 1);
}
for (int u : hab[i]) if (p[u] >= SQRT)
{
addedge(i, 0, u, SQRT, 0);
}
}
FOR(i, 1, m + 1) if (p[i] >= SQRT)
{
for (int j = B[i]; j <= n; j += p[i])
{
addedge(i, SQRT, j, 0, (j - B[i]) / p[i]);
}
for (int j = B[i] - p[i]; j >= 1; j -= p[i])
{
addedge(i, SQRT, j, 0, (B[i] - j) / p[i]);
}
}
*/
dij(); // from (B[1], 0)
if (dist[B[2]][0] >= INF) cout << "-1\n";
else cout << dist[B[2]][0] << endl;
return 0;
}
# | 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... |