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>
#define ll long long
#define pb push_back
#define task "slingshot"
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define fi first
#define se second
#define ull unsigned long long
using namespace std;
const ll mod = 1e12;
const ll N = 1e5+5;
const int base = 300;
ll n, m, t, ans, k, a[N], b[N], c[N], tong, cnt, q, d[N], P[N][21], h[N], lab[N], dp[N][200];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
struct point
{
ll x, y;
}p[N];
vector<ll> dx, dy;
vector<ll> adj[N], kq, gr[N];
string s[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
ll A(ll u, ll v)
{
if(u > v)return 0;
return b[v] * c[v-u] % mod;
}
ll findp(ll u)
{
return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(ll u, ll v)
{
u = findp(u);
v = findp(v);
if(u == v)return;
if(lab[u] > lab[v])swap(u, v);
lab[u] += lab[v];
lab[v] = u;
}
void add(ll id)
{
for(; id <= n;id += id & -id)++d[id];
}
ll get(ll id)
{
ll total = 0;
for(; id; id -= id & -id)total += d[id];
return total;
}
ll lwr(ll x)
{
return lower_bound(kq.begin(), kq.end(), x) - kq.begin() + 1;
}
void dfs(ll u, ll p)
{
a[u] = ++m;
for(int i = 1; i <= 20; i ++)P[u][i] = P[P[u][i-1]][i-1];
for(ll v : adj[u])
{
if(v == p)continue;
h[v] = h[u] + 1;
P[v][0] = u;
dfs(v, u);
}
}
bool cmp(const ll& x, const ll& y)
{
return a[x] < a[y];
}
ll lca(ll u, ll v)
{
if(h[u] < h[v])swap(u, v);
ll log = log2(h[u]);
for(int i = log; i >= 0; i --)if(h[u] >= h[v] + (1<<i))u = P[u][i];
if(u == v)return u;
for(int i = log; i >= 0; i --)
{
if(P[u][i] && P[u][i] != P[v][i])
{
u = P[u][i];
v = P[v][i];
}
}
return P[u][0];
}
void cal(ll u, ll p)
{
for(ll v : adj[u])
{
if(v == p)continue;
cal(v, u);
c[u] += c[v];
}
if(c[u])hop(u, P[u][0]);
}
void sol()
{
cin >> m >> n;
k = 180;
for(int i = 0; i < n; i ++)
{
cin >> a[i] >> b[i];
adj[a[i]].pb(b[i]);
}
for(int i = 0; i < m; i ++)for(int j = 0; j <= k; j ++)dp[i][j] = mod;
priority_queue< pii, vector<pii>, greater<pii> > pq;
dp[a[0]][0] = 0;
pq.push({0, {a[0], 0}});
while(!pq.empty())
{
pii u = pq.top();
pq.pop();
if(u.fi != dp[u.se.fi][u.se.se])continue;
if(!u.se.se)
{
for(ll v : adj[u.se.fi])
{
if(v < k)
{
if(dp[u.se.fi][v] > u.fi)
{
dp[u.se.fi][v] = u.fi;
pq.push({u.fi, {u.se.fi, v}});
}
}
else
{
for(int j = u.se.fi - v, c = 1; j >= 0; j -= v, c++)
{
if(dp[j][0] > u.fi + c)
{
dp[j][0] = u.fi + c;
pq.push({dp[j][0], {j, 0}});
}
}
for(int j = u.se.fi + v, c = 1; j < m; j += v, c ++)
{
if(dp[j][0] > u.fi + c)
{
dp[j][0] = u.fi + c;
pq.push({dp[j][0], {j, 0}});
}
}
}
}
}
else
{
if(u.se.fi + u.se.se < m && dp[u.se.fi+u.se.se][u.se.se] > u.fi+1)
{
dp[u.se.fi+u.se.se][u.se.se] = u.fi+1;
pq.push({u.fi+1, {u.se.fi+u.se.se, u.se.se}});
}
if(u.se.fi - u.se.se >= 0 && dp[u.se.fi-u.se.se][u.se.se] > u.fi+1)
{
dp[u.se.fi-u.se.se][u.se.se] = u.fi+1;
pq.push({u.fi+1, {u.se.fi-u.se.se, u.se.se}});
}
if(dp[u.se.fi][0] > u.fi)
{
dp[u.se.fi][0] = u.fi;
pq.push({u.fi, {u.se.fi, 0}});
}
}
}
ans = dp[a[1]][0];
if(ans == mod)ans = -1;
cout << ans;
}
int main()
{
if(fopen(task".in", "r")){
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:187:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
187 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:188:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
188 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |