//https://oj.uz/problem/view/APIO15_skyscraper
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pii;
const int k = 200;
const int oo = 1e9 + 5;
const int maxn = 3e4 + 5;
int n, m, d[maxn], vs[maxn], ss, ee;
set<int> st[2][maxn];
int dij() {
priority_queue<pii> heap;
rep(i, 0, n) d[i] = oo;
d[ss] = 0;
heap.push(mp(0, ss));
while (!heap.empty()) {
pii cur = heap.top();
cur.fi *= -1;
if (cur.se == ee) return d[cur.se];
// cout << "here " << cur.se << endl;
heap.pop();
if (vs[cur.se]) continue;
vs[cur.se] = 1;
for (auto it = st[0][cur.se].begin(); it != st[0][cur.se].end(); it++) {
int jump = *it;
int now = cur.se;
while (cur.se + jump < n) {
int nxt = cur.se + jump;
if (d[nxt] > cur.fi + 1) {
d[nxt] = cur.fi + 1;
heap.push(mp(-d[nxt], nxt));
st[0][nxt].insert(jump);
}
if (st[0][nxt].find(jump) != st[0][nxt].end()) break;
}
cur.se = now;
while (cur.se - jump >= 0) {
int pre = cur.se - jump;
if (d[pre] > cur.fi + 1) {
d[pre] = cur.fi + 1;
heap.push(mp(-d[pre], pre));
st[0][pre].insert(jump);
}
if (st[0][pre].find(jump) != st[0][pre].end()) break;
}
}
}
return -1;
}
int main() {
// freopen("inp.txt", "r", stdin);
cin >> n >> m;
rep(i, 0, m) {
int x, y;
cin >> x >> y;
if (i == 0) ss = x;
if (i == 1) ee = x;
st[0][x].insert(y);
// else st[1][x].insert(y);
}
cout << dij();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
3196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
3196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1076 ms |
3244 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1073 ms |
3288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1077 ms |
3288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |