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];
priority_queue< pair < LL , LL > > q;
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 = 0; 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] });
}
}
}
for( int i = 0; i <= n; i++ ){
sort( v[i].begin() , v[i].end() );
}
d[a[0]] = 0;
q.push({0 , a[0]});
while( !q.empty() ){
int x = q.top().se;
LL f = q.top().fi;
q.pop();
if( f > d[x] )continue;
for( auto y : v[x] ){
if( d[x] + y.se < d[y.fi] ){
d[y.fi] = d[x] + y.se;
q.push({ d[y.fi] , y.fi });
}
}
}
if( d[a[1]] == 2e18 )d[a[1]] = -1;
cout << d[a[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... |