#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n , m;
cin >> n >> m;
vector<vector<pair<int,int>>> adj(n + 1);
const int N = 30000 + 10;
bitset<N> vis[N][2];
int s = -1 , d = -1;
for(int i = 0 ; i < m ; i++){
int a , b;
cin >> a >> b;
if(s == -1) s = a;
else if(d == -1) d = a;
if(vis[a][b][0] && vis[a][b][1]) continue;
vis[a][b][0] = true; vis[a][b][1] = true;
for(int j = a + b , cnt = 1 ; j < n ; j += b , cnt++){
adj[a].emplace_back(j , cnt);
if(vis[j][b][1]) break;
vis[j][b][1] = true;
}
for(int j = a - b , cnt = 1 ; j >= 0 ; j -= b , cnt++){
adj[a].emplace_back(j , cnt);
if(vis[j][b][0]) break;
vis[j][b][0] = true;
}
}
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
pq.emplace(0 , s);
vector<int> dist(n + 1 , (int)1e9);
dist[s] = 0;
while(!pq.empty()){
auto f = pq.top();
pq.pop();
if(f.second == d) break;
if(f.first != dist[f.second]) continue;
for(auto x : adj[f.second]){
int node = x.first;
int cost = x.second;
if(dist[node] > f.first + cost){
dist[node] = f.first + cost;
pq.emplace(dist[node] , node);
}
}
}
if(dist[d] == (int)1e9) cout << -1 << '\n';
else cout << dist[d] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
220636 KB |
Output is correct |
2 |
Correct |
105 ms |
220560 KB |
Output is correct |
3 |
Correct |
103 ms |
220628 KB |
Output is correct |
4 |
Correct |
114 ms |
220612 KB |
Output is correct |
5 |
Correct |
104 ms |
220548 KB |
Output is correct |
6 |
Correct |
115 ms |
220632 KB |
Output is correct |
7 |
Correct |
102 ms |
220564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
220564 KB |
Output is correct |
2 |
Correct |
108 ms |
220588 KB |
Output is correct |
3 |
Correct |
113 ms |
220540 KB |
Output is correct |
4 |
Correct |
104 ms |
220520 KB |
Output is correct |
5 |
Correct |
109 ms |
220628 KB |
Output is correct |
6 |
Correct |
103 ms |
220612 KB |
Output is correct |
7 |
Correct |
113 ms |
220728 KB |
Output is correct |
8 |
Correct |
105 ms |
220524 KB |
Output is correct |
9 |
Correct |
102 ms |
220612 KB |
Output is correct |
10 |
Incorrect |
102 ms |
220596 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
220636 KB |
Output is correct |
2 |
Correct |
106 ms |
220740 KB |
Output is correct |
3 |
Correct |
102 ms |
220604 KB |
Output is correct |
4 |
Correct |
103 ms |
220616 KB |
Output is correct |
5 |
Correct |
106 ms |
220616 KB |
Output is correct |
6 |
Correct |
102 ms |
220564 KB |
Output is correct |
7 |
Correct |
114 ms |
220644 KB |
Output is correct |
8 |
Correct |
103 ms |
220572 KB |
Output is correct |
9 |
Correct |
111 ms |
220532 KB |
Output is correct |
10 |
Incorrect |
101 ms |
220628 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
220748 KB |
Output is correct |
2 |
Correct |
109 ms |
220684 KB |
Output is correct |
3 |
Correct |
101 ms |
220632 KB |
Output is correct |
4 |
Correct |
106 ms |
220616 KB |
Output is correct |
5 |
Correct |
104 ms |
220560 KB |
Output is correct |
6 |
Correct |
105 ms |
220584 KB |
Output is correct |
7 |
Correct |
111 ms |
220692 KB |
Output is correct |
8 |
Correct |
121 ms |
220520 KB |
Output is correct |
9 |
Correct |
102 ms |
220532 KB |
Output is correct |
10 |
Incorrect |
105 ms |
220612 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
220528 KB |
Output is correct |
2 |
Correct |
106 ms |
220620 KB |
Output is correct |
3 |
Correct |
110 ms |
220616 KB |
Output is correct |
4 |
Correct |
109 ms |
220636 KB |
Output is correct |
5 |
Correct |
103 ms |
220524 KB |
Output is correct |
6 |
Correct |
107 ms |
220740 KB |
Output is correct |
7 |
Correct |
103 ms |
220636 KB |
Output is correct |
8 |
Correct |
111 ms |
220528 KB |
Output is correct |
9 |
Correct |
107 ms |
220552 KB |
Output is correct |
10 |
Incorrect |
111 ms |
220648 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |