#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(int& a, int b){
a = max(a, b);
}
void selfMin(int& a, int 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<int> e;
DSU(int N) { e = vector<int>(N, -1); }
// get representive component (uses path compression)
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int 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<int> BIT
*/
template <class T> class BIT {
private:
int size;
vector<T> bit;
vector<T> arr;
public:
BIT(int size) : size(size), bit(size + 1), arr(size) {}
void set(int ind, int val) { add(ind, val - arr[ind]); }
void add(int ind, int val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(int 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<int> Seg;
*/
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
int n; vector<T> seg;
void init(int _n) { n = _n; seg.assign(2*n,ID); }
void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(int p, T val) {
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(int l, int 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(){
int n, m;
cin >> n >> m;
vector<pair<int, int> > dogs(m);
for(int i = 0; i < m; i++){
cin >> dogs[i];
}
map<pair<int, int>, int> mp;
for(int i = 0; i < m; i++){
mp[dogs[i]]++;
}
vector<pair<int,int> > adj[n];
for(int 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));
int 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));
int 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<int, int> > 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(0LL, dogs[0].first));
while(!pq.empty()){
pair<long long, int> x2 = pq.top();
pq.pop();
if(distances[x2.second] != -x2.first){
continue;
}
int u = x2.second;
for(pair<int, int> x : adj[u]){
int v = x.first;
if(distances[v] > distances[u] + (long long)x.second){
distances[v] = distances[u] + (long long)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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
2472 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
2504 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
580 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
432 KB |
Output is correct |
24 |
Correct |
3 ms |
584 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
67 ms |
33420 KB |
Output is correct |
27 |
Correct |
70 ms |
33356 KB |
Output is correct |
28 |
Correct |
3 ms |
700 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
732 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
4 ms |
1844 KB |
Output is correct |
34 |
Correct |
5 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
436 KB |
Output is correct |
12 |
Correct |
4 ms |
2504 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
2 ms |
436 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
428 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
68 ms |
33316 KB |
Output is correct |
27 |
Correct |
70 ms |
33408 KB |
Output is correct |
28 |
Correct |
3 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
4 ms |
1856 KB |
Output is correct |
34 |
Correct |
4 ms |
1972 KB |
Output is correct |
35 |
Correct |
26 ms |
3388 KB |
Output is correct |
36 |
Correct |
4 ms |
852 KB |
Output is correct |
37 |
Correct |
32 ms |
4096 KB |
Output is correct |
38 |
Correct |
37 ms |
4596 KB |
Output is correct |
39 |
Correct |
37 ms |
4568 KB |
Output is correct |
40 |
Correct |
36 ms |
4624 KB |
Output is correct |
41 |
Correct |
38 ms |
4544 KB |
Output is correct |
42 |
Runtime error |
353 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
2488 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
568 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
67 ms |
33368 KB |
Output is correct |
27 |
Correct |
69 ms |
33368 KB |
Output is correct |
28 |
Correct |
3 ms |
696 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
4 ms |
1840 KB |
Output is correct |
34 |
Correct |
5 ms |
1972 KB |
Output is correct |
35 |
Correct |
28 ms |
3480 KB |
Output is correct |
36 |
Correct |
4 ms |
880 KB |
Output is correct |
37 |
Correct |
33 ms |
4028 KB |
Output is correct |
38 |
Correct |
46 ms |
4524 KB |
Output is correct |
39 |
Correct |
37 ms |
4596 KB |
Output is correct |
40 |
Correct |
36 ms |
4584 KB |
Output is correct |
41 |
Correct |
37 ms |
4484 KB |
Output is correct |
42 |
Runtime error |
343 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |