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 int long long
#define endl '\n'
#define pii pair<int, int>
const int MX = 3e5 + 5;
int INF;
int vi[MX], b[MX], p[MX];
int d[MX];
int n, m;
// ------------ FUNCTIONS -----------
void bfs(){
memset(d, 63, sizeof d);
INF = d[0];
priority_queue<pii> q;
q.push({0, 1});
d[b[1]] = 0;
int c = 0;
while(q.size() && c <= 10){
++c;
int w, u;
tie(w, u) = q.top();
w = -w;
q.pop();
for(int i=1; i<=m; ++i){
int v = b[i];
int c = abs(b[u] - v) / p[u];
if(abs(b[u] - v) % p[u] == 0 && w + c < d[v]){
d[v] = w + c;
q.push({-d[v], i});
}
}
}
}
// ------------ SUBTASKS ------------
namespace SUB123{
bool ok(){
return (n <= 2000 && m <= 2000);
}
void solve(){
bfs();
cout<<(d[b[2]] == INF ? -1 : d[b[2]]);
//---------------------
exit(0);
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("main.inp","r",stdin);
//freopen("main.out","w",stdout);
cin>>n>>m;
for(int i=1; i<=m; ++i){
cin>>b[i]>>p[i];
}
if(SUB123 :: ok()){
SUB123 :: solve();
return 0;
}
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... |