#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 3e4;
const int inf = 1e9;
vector<pii> edges[MAXN];
int n, m;
int dist[MAXN];
vector<int> jump_occs[MAXN+1];
int occ_pos[MAXN+1];
vector<int> valsat[MAXN];
void dijkstra(int s) {
fill(dist, dist+n, inf);
dist[s] = 0;
priority_queue<pii> pq;
pq.push(pii(0, s));
while (!pq.empty()) {
pii p = pq.top();
pq.pop();
int cur = p.second;
if (dist[cur] < p.first) continue;
for (pii e: edges[cur]) {
if (dist[cur]+e.second < dist[e.first]) {
dist[e.first] = dist[cur]+e.second;
pq.push(pii(dist[e.first], e.first));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m;
int posf;
int poss;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
if (i == 1) posf = x;
if (i == 0) poss = x;
jump_occs[y].push_back(x);
occ_pos[y]++;
valsat[x].push_back(y);
//at[x].insert(y);
}
for (int i = n-1; i >= 0; i--) {
for (int p: valsat[i]) {
occ_pos[p]--;
int cur = occ_pos[p];
int psize = jump_occs[p].size();
for (int j = i+p; j < n; j += p) {
edges[i].push_back(pii(j, (j-i)/p));
while (cur < psize && jump_occs[p][cur] < j) cur++;
if (cur < psize && jump_occs[p][cur] == j) break;
}
}
}
for (int i = 0; i < n; i++) {
for (int p: valsat[i]) {
int cur = occ_pos[p];
for (int j = i-p; j >= 0; j -= p) {
edges[i].push_back(pii(j, (i-j)/p));
while (cur >= 0 && jump_occs[p][cur] > j) cur--;
if (cur >= 0 && jump_occs[p][cur] == j) break;
}
occ_pos[p]++;
}
}
dijkstra(poss);
cout << ((dist[posf] == inf) ? (-1) : dist[posf]) << "\n";
return 0;
}
Compilation message
sculpture.cpp: In function 'int main()':
sculpture.cpp:72:10: warning: 'poss' may be used uninitialized in this function [-Wmaybe-uninitialized]
72 | dijkstra(poss);
| ~~~~~~~~^~~~~~
sculpture.cpp:73:21: warning: 'posf' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | cout << ((dist[posf] == inf) ? (-1) : dist[posf]) << "\n";
| ~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |