#include <bits/stdc++.h>
/*
--> Author: Kazuki_Hoshino__8703 <--
I love Nanasaki Ai ☆*: .。. o(≒_≒)o .。.:☆
*/
#define fi first
#define se second
#define pii pair<int, int>
#define ll long long
using namespace std;
const int mn = 3e4 + 5, bm = (1 << 11) + 1, mod = 1532023, offset = 5e4;
const int inf = 1e9, base = 311;
int n, m, a[mn], b[mn];
int d[mn][mn];
vector <int> speed[mn];
void dijkstra(){
queue<pair<int, pii>> pq;
fill(&d[0][0], &d[0][0] + mn * mn, inf);
d[a[0]][b[0]] = 0;
pq.push({d[a[0]][b[0]], {a[0], b[0]}});
while(pq.size()){
auto[c, i] = pq.front();
auto[u, sp] = i;
pq.pop();
if(c > d[u][sp]) continue;
if(u + sp < n && d[u + sp][sp] > d[u][sp] + 1){
d[u + sp][sp] = d[u][sp] + 1;
pq.push({d[u + sp][sp], {u + sp, sp}});
}
if(u - sp >= 0 && d[u - sp][sp] > d[u][sp] + 1){
d[u - sp][sp] = d[u][sp] + 1;
pq.push({d[u - sp][sp], {u - sp, sp}});
}
for(auto qq : speed[u]){
if(d[u][qq] > d[u][sp]){
d[u][qq] = d[u][sp];
pq.push({d[u][qq], {u, qq}});
}
}
}
int res = inf;
for(int sp = 0; sp <= 3e4; sp++) res = min(res, d[a[1]][sp]);
if(res == inf) res = -1;
cout << res << '\n';
}
void solve(){
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i];
speed[a[i]].push_back(b[i]);
}
dijkstra();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if(fopen("ROBOT.INP", "r")) {
freopen("ROBOT.INP", "r", stdin);
freopen("ROBOT.OUT", "w", stdout);
}
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("ROBOT.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen("ROBOT.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/ccAoANI6.o: in function `dijkstra()':
skyscraper.cpp:(.text+0x39c): relocation truncated to fit: R_X86_64_PC32 against symbol `a' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x3a3): relocation truncated to fit: R_X86_64_PC32 against symbol `b' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x4a3): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x5db): relocation truncated to fit: R_X86_64_PC32 against symbol `a' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x61e): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.a(globals_io.o)
/tmp/ccAoANI6.o: in function `solve()':
skyscraper.cpp:(.text+0x6b9): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x6ce): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.a(globals_io.o)
skyscraper.cpp:(.text+0x6d9): relocation truncated to fit: R_X86_64_PC32 against symbol `b' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x6e1): relocation truncated to fit: R_X86_64_PC32 against symbol `a' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x6ed): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccAoANI6.o
skyscraper.cpp:(.text+0x6fb): additional relocation overflows omitted from the output
/usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.a(ios_init.o): in function `std::ios_base::Init::Init()':
(.text._ZNSt8ios_base4InitC2Ev+0x1c): failed to convert GOTPCREL relocation against '_ZNSt8ios_base4Init11_S_refcountE'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x1c6): failed to convert GOTPCREL relocation against '_ZSt4cout'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x260): failed to convert GOTPCREL relocation against '_ZSt3cin'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x2e2): failed to convert GOTPCREL relocation against '_ZSt4cerr'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x353): failed to convert GOTPCREL relocation against '_ZSt4clog'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x541): failed to convert GOTPCREL relocation against '_ZSt5wcout'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x5e5): failed to convert GOTPCREL relocation against '_ZSt4wcin'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x670): failed to convert GOTPCREL relocation against '_ZSt5wcerr'; relink with --no-relax
(.text._ZNSt8ios_base4InitC2Ev+0x6e9): failed to convert GOTPCREL relocation against '_ZSt5wclog'; relink with --no-relax
/usr/bin/ld: final link failed
collect2: error: ld returned 1 exit status