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 LL long long
#define se second
#define fi first
using namespace std;
const int N = 3e4 + 7;
const int mod = 1e9 + 7;
const int B = sqrt(N);
LL n,m;
LL a[N];
LL b[N];
LL d[N];
vector < pair < LL , LL > > v[N];
int main()
{
ios_base::sync_with_stdio(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
cin >> n >> m;
for( int i = 0; i < m; i++ ){
cin >> a[i] >> b[i];
}
for( int i = 1; i <= n; i++ )d[i] = 2e18;
for( int i = 0; i < m; i++ ){
for( int j = 0; j < m; j++ ){
if( i == j )continue;
if( abs( a[i] - a[j] ) % b[i] == 0 ){
v[a[i]].push_back({ a[j] , abs( a[i] - a[j] ) / b[i] });
}
}
}
queue < LL > q;
q.push(0);
d[0] = 0;
while( !q.empty() ){
LL x = q.front();
q.pop();
for( auto y : v[x] ){
if( d[x] + y.se < d[y.fi] ){
q.push(y.fi);
d[y.fi] = d[x] + y.se;
}
}
}
if( d[1] == 2e18 )d[1] = -1;
cout << d[1];
}
# | 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... |