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>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast,unroll-loops")
#define ll long long
#define all(x) x.begin(), x.end()
#define ld long double
#define pii pair<int, int>
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// ifstream cin("input.txt");
// ofstream cout("output.txt");
int n, m;
cin >> n >> m;
vector<int> b(m);
vector<int> p(m);
vector<int> d(n, INF);
vector<vector<int> > g(n);
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
g[b[i]].push_back(p[i]);
}
d[b[0]] = 0;
set<pair<int, int> > s;
s.insert({0, b[0]});
while(!s.empty()){
int cv = s.begin() -> second;
int cd = s.begin() -> first;
s.erase(s.begin());
for(auto i: g[cv]){
int cx = cv;
int cy = 0;
while(cx >= 0){
if(cd + cy < d[cx]){
s.erase({d[cx], cx});
d[cx] = cd + cy;
s.insert({d[cx], cx});
}
cx -= i;
cy++;
}
cx = cv;
cy = 0;
while(cx < n){
if(cd + cy < d[cx]){
s.erase({d[cx], cx});
d[cx] = cd + cy;
s.insert({d[cx], cx});
}
cx += i;
cy++;
}
}
}
if(d[b[1]] == INF){
cout << "-1" << endl;
}
else{
cout << d[b[1]] << endl;
}
}
# | 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... |