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 IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
a = max(a, b);
}
void selfMin(long long& a, long long b){
a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};
/*
Use DSU dsu(N);
*/
struct DSU {
vector<long long> e;
DSU(long long N) { e = vector<long long>(N, -1); }
// get representive component (uses path compression)
long long get(long long x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(long long a, long long b) { return get(a) == get(b); }
long long size(long long x) { return -e[get(x)]; }
bool unite(long long x, long long y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
}
};
/*
Run with Bit<long long> BIT
*/
template <class T> class BIT {
private:
long long size;
vector<T> bit;
vector<T> arr;
public:
BIT(long long size) : size(size), bit(size + 1), arr(size) {}
void set(long long ind, long long val) { add(ind, val - arr[ind]); }
void add(long long ind, long long val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(long long ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
/*
Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;
*/
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
long long n; vector<T> seg;
void init(long long _n) { n = _n; seg.assign(2*n,ID); }
void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(long long p, T val) {
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(long long l, long long r) {
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
int main(){
long long n, m;
cin >> n >> m;
vector<pair<long long, long long> > dogs(m);
for(long long i = 0; i < m; i++){
cin >> dogs[i];
}
map<pair<long long, long long>, long long> mp;
for(long long i = 0; i < m; i++){
mp[dogs[i]]++;
}
vector<pair<long long,long long> > adj[n];
for(long long i = 0; i < m; i++){
if(0 <= dogs[i].first + dogs[i].second && dogs[i].first + dogs[i].second < n){
adj[dogs[i].first].push_back(make_pair(dogs[i].second + dogs[i].first, 1));
long long k = 2;
while(0 <= dogs[i].first + dogs[i].second * k && dogs[i].first + dogs[i].second * k < n && mp.count(make_pair(dogs[i].first + dogs[i].second * k, dogs[i].second)) == 0){
adj[dogs[i].first].push_back(make_pair(dogs[i].first + dogs[i].second * k, k));
k++;
}
if(0 <= dogs[i].first + dogs[i].second * k && dogs[i].first + dogs[i].second * k < n){
adj[dogs[i].first].push_back(make_pair(dogs[i].first + dogs[i].second * k, k));
}
}
if(0 <= dogs[i].first - dogs[i].second && dogs[i].first - dogs[i].second < n){
adj[dogs[i].first].push_back(make_pair(dogs[i].first - dogs[i].second, 1));
long long k = 2;
while(0 <= dogs[i].first - dogs[i].second * k && dogs[i].first - dogs[i].second * k < n && mp.count(make_pair(dogs[i].first - dogs[i].second * k, dogs[i].second)) == 0){
adj[dogs[i].first].push_back(make_pair(dogs[i].first - dogs[i].second * k, k));
k++;
}
if(0 <= dogs[i].first - dogs[i].second * k && dogs[i].first - dogs[i].second * k < n){
adj[dogs[i].first].push_back(make_pair(dogs[i].first - dogs[i].second * k, k));
}
}
}
priority_queue<pair<long long, long long> > pq;
vector<long long> distances(n, 1e18);
if(!(0 <= dogs[0].first && dogs[0].first < n)){
cout << -1 << "\n";
return 0;
}
if(!(0 <= dogs[1].first && dogs[1].first < n)){
cout << -1 << "\n";
return 0;
}
distances[dogs[0].first] = 0;
pq.push(make_pair(0, dogs[0].first));
while(!pq.empty()){
pair<long long, long long> x2 = pq.top();
pq.pop();
if(distances[x2.second] != x2.first){
continue;
}
long long u = x2.second;
for(pair<long long, long long> x : adj[u]){
long long v = x.first;
if(distances[v] > distances[u] + x.second){
distances[v] = distances[u] + x.second;
pq.push(make_pair(distances[v], v));
}
}
}
if(distances[dogs[1].first] == (long long)1e18){
cout << -1 << "\n";
}
else{
cout << distances[dogs[1].first] << "\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... |