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>
//# pragma GCC optimize("Ofast")
# define pb push_back
# define ll long long
# define fi first
# define se second
# define all(vc) vc.begin(),vc.end()
# define forn(i, n) for (int i = 0; i < int(n); i++)
# define ford(i, n) for (int i = int(n) - 1; i >= 0; i--)
# define fore(i, l, r) for (int i = int(l); i < int(r); i++)
# define in freopen("out.txt", "r", stdout)
# define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
# define time cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"
using namespace std;
const ll INF = 1e18;
const int mod = 1e9 + 7;
const ll N = 1e6 + 7;
typedef vector < vector < ll > > matrix;
int main()
{
speed;
int n, m;
cin >> n >> m;
vector < pair < int, int > > vc(m);
map < int, int > mp;
forn( i, m ) {
cin >> vc[i].fi >> vc[i].se;
mp[vc[i].fi] = vc[i].se;
}
vector < int > g[n + 7], used(n + 7), w(n + 7);
forn( i, m ) {
forn( j, m ) {
if ( i == j ) continue;
if ( abs( vc[i].fi - vc[j].fi ) % vc[i].se == 0 ) {
g[ vc[i].fi ].pb( vc[j].fi );
}
}
}
used[vc[0].fi] = 1;
queue < int > q;
q.push(vc[0].fi);
w[vc[0].fi] = 0;
while ( !q.empty() ) {
int from = q.front();
for ( auto to: g[from] ) {
if ( !used[to] ) {
w[to] = abs(from - to) / mp[from] + w[from];
used[to] = 1;
q.push(to);
}
}
q.pop();
}
if ( w[vc[1].fi] == 0 ) cout << -1;
else cout << w[vc[1].fi];
time;
}
# | 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... |