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 = 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] });
}
}
}
d[a[0]] = 0;
set < pair < LL , LL > > s;
s.insert({d[a[0]] , 0});
while( !s.empty() ){
LL x = s.begin()->fi;
s.erase(s.begin());
for( auto y : v[x] ){
if( d[x] + y.se < d[y.fi] ){
s.erase({ y.fi , d[y.fi] });
d[y.fi] = d[x] + y.se;
s.insert({y.fi , d[y.fi]});
}
}
}
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... |