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 fi first
#define se second
#define LL long long
using namespace std;
const int B = 317;
const int N = 3e4 + 7;
const int maxn = 1e5 + 7;
const LL mod = 1e9 + 7;
const LL mod1 = 1e9 + 9;
const int inf = 1e9 + 100;
int n,m;
pair < int , int > a[N];
int d[N];
vector < pair < int , int > > v[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
cin >> n >> m;
set < int > g;
for( int i = 0; i < m; i++ ){
cin >> a[i].fi >> a[i].se;
g.insert(a[i].fi);
}
sort( a , a + m );
for( int i = 0; i <= n; i++ )d[i] = 1e9;
for( int i = 0; i < m; i++ ){
for( int j = i + 1; j < m; j++ ){
if( abs(a[i].fi - a[j].fi) % a[i].se == 0 ){
v[a[i].fi].push_back({ a[j].fi , abs(a[i].fi - a[j].fi) / a[i].se });
v[a[j].fi].push_back({ a[i].fi , abs(a[i].fi - a[j].fi) / a[j].se });
break;
}
}
}
d[a[0].fi] = 0;
set < pair < int , int > > s;
s.insert({a[0].fi , 0});
while( !s.empty() ){
int 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[a[1].fi] == 1e9 )d[a[1].fi] = -1;
cout << d[a[1].fi];
//cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
# | 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... |