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;
#define ll long long
#define endl '\n'
const int N = 5e5 + 5;
const int root = 200;
ll dp[30005], vis[30005], u[30005], v[30005];
bool ok[30005][205];
vector<ll> E[30005];
struct viet{
ll u, v, c;
bool operator <(const viet& o) const{
return c > o.c;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
ll n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> u[i] >> v[i];
u[i]++;
E[u[i]].push_back(i);
}
priority_queue<viet> q;
for (int i = 1; i <= n; i++){
dp[i] = 1e9;
}
for (auto j : E[u[1]]){
q.push({u[j], v[j], 0});
dp[u[j]] = 0;
ok[u[j]][v[j]] = 1;
}
vis[1] = 1;
while (q.size()){
viet x = q.top();
q.pop();
if (x.v < root){
if ((x.u - x.v) > 0){
if (vis[x.u - x.v] == 0){
vis[x.u - x.v] = 1;
dp[x.u - x.v] = x.c + 1;
for (auto j : E[x.u - x.v]){
if (v[j] < root) ok[x.u - x.v][v[j]] = 1;
q.push({x.u - x.v, v[j], x.c + 1});
}
q.push({x.u - x.v, x.v, x.c + 1});
} else if (ok[x.u - x.v][x.v] == 0){
ok[x.u - x.v][x.v] = 1;
q.push({x.u - x.v, x.v, x.c + 1});
}
}
if ((x.u + x.v) <= n){
if (vis[x.u + x.v] == 0){
vis[x.u + x.v] = 1;
dp[x.u + x.v] = x.c + 1;
for (auto j : E[x.u + x.v]){
if (v[j] < root) ok[x.u + x.v][v[j]] = 1;
q.push({x.u + x.v, v[j], x.c + 1});
}
q.push({x.u + x.v, x.v, x.c + 1});
} else if (ok[x.u + x.v][x.v] == 0){
ok[x.u + x.v][x.v] = 1;
q.push({x.u + x.v, x.v, x.c + 1});
}
}
} else {
for (int i = 1; i <= root; i++){
if ((x.u + x.v * i) > n) break;
if (vis[x.u + x.v * i] == 0){
vis[x.u + x.v * i] = 1;
dp[x.u + x.v * i] = min(dp[x.u + x.v * i], x.c + i);
for (auto j : E[x.u + x.v * i]){
if (v[j] < root) ok[x.u + x.v * i][v[j]] = 1;
q.push({x.u + x.v * i, v[j], x.c + i});
}
} else if (dp[x.u + x.v * i] > (x.c + i)){
dp[x.u + x.v * i] = min(dp[x.u + x.v * i], x.c + i);
for (auto j : E[x.u + x.v * i]){
if (v[j] < root) ok[x.u + x.v * i][v[j]] = 1;
q.push({x.u + x.v * i, v[j], x.c + i});
}
}
}
for (int i = 1; i <= root; i++){
if ((x.u - x.v * i) <= 0) break;
if (vis[x.u - x.v * i] == 0){
vis[x.u - x.v * i] = 1;
dp[x.u - x.v * i] = min(dp[x.u - x.v * i], x.c + i);
for (auto j : E[x.u - x.v * i]){
if (v[j] < root) ok[x.u - x.v * i][v[j]] = 1;
q.push({x.u - x.v * i, v[j], x.c + i});
}
} else if (dp[x.u - x.v * i] > (x.c + i)){
dp[x.u - x.v * i] = min(dp[x.u - x.v * i], x.c + i);
for (auto j : E[x.u - x.v * i]){
if (v[j] < root) ok[x.u - x.v * i][v[j]] = 1;
q.push({x.u - x.v * i, v[j], x.c + i});
}
}
}
}
}
ll ans = dp[u[2]];
if (ans == 1e9) cout << -1;
else cout << ans;
}
/*
*/
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:22:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
22 | freopen("VietCT.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:23:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
23 | freopen("VietCT.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... |