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>
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long
template <class _T>
bool chmin(_T &x, const _T &y){
bool flag = false;
if ( x > y ){
x = y; flag |= true;
}
return flag;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag = false;
if ( x < y ){
x = y; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
set <int> s[n + 1];
int st = -1, en = -1;
for ( int i = 1; i <= m; i++ ){
int pos, v; cin >> pos >> v;
st = i == 1 ? pos : st;
en = i == 2 ? pos : en;
s[pos].insert(v);
}
vector <pair<int,int>> g[n];
for ( int i = 0; i < n; i++ ){
for ( auto d: s[i] ){
for ( int j = i - d; j >= 0; j -= d ){
g[i].pb({j, (i - j) / d});
if ( s[j].count(d) ) break;
}
for ( int j = i + d; j < n; j += d ){
g[i].pb({j, (j - i) / d});
if ( s[j].count(d) ) break;
}
}
}
const int inf = 1e17 + 1;
vector <int> dis(n, inf);
dis[st] = 0;
priority_queue <pair<int,int>> q;
q.push({0, st});
while ( !q.empty() ){
auto [val, x] = q.top();
q.pop(); val = -val;
if ( val > dis[x] ) continue;
for ( auto [to, w]: g[x] ){
if ( chmin(dis[to], w + val) ){
q.push({-dis[to], to});
}
}
}
int res = dis[en];
cout << (res == inf ? -1 : res);
cout << '\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... |