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 ll long long
#define pb push_back
#define bug cout << "bug" << endl
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define pll pair <ll, ll>
#define pii pair <int, int>
#define triple pair <pair <ll, ll> , ll>
#define ull unsigned long long
#define ld long double
#define pinode pair <node*, node*>
const ll INF = 9e18 + 5;
const ll inf = 1e9 + 5;
const ll N = 2e3 + 5;
const ll shift = 2e6;
const ll mod = 998244353;
const ll mod2 = 1e9 + 9;
const ll M = 1e3 + 5;
const ll LOG = 21;
const ll sp = 263;
const ll sp2 = 9973;
const int block = 100;
const double eps = 1e-10;
ll n, m, b[N], p[N], d[N];
vector <pii> g[N];
int main(){
speed;
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
}
for(int i = 0; i < m; i++){
if(i == 1)
continue;
for(int j = 0; j < m; j++){
if(abs(b[i] - b[j]) % p[i] == 0){
g[i].pb({j, abs(b[i] - b[j]) / p[i]});
}
}
}
for(int i = 0; i < m; i++){
d[i] = INF;
}
set <pii> st;
st.insert({0, 0});
d[0] = 0;
while(st.size()){
ll v = st.begin() -> second, len = st.begin() -> first;
st.erase(st.begin());
for(auto to : g[v]){
if(d[v] + to.S < d[to.F]){
st.erase({d[to.F], to.F});
d[to.F] = d[v] + to.S;
st.insert({d[to.F], to.F});
}
}
}
if(d[1] == INF)
d[1] = -1;
cout << d[1] << endl;
}
/*
%I64d6
6 1 3
8 1 2 1 5 4
%I64d
*/
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:57:32: warning: unused variable 'len' [-Wunused-variable]
57 | ll v = st.begin() -> second, len = st.begin() -> first;
| ^~~
# | 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... |