#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 3e4 + 7;
const int M = 3e4 + 7;
const int Q = 175;
int n, m, a[M], b[M];
vector<int> v[N];
int col0 = 0, col1 = 1;
int d[N][Q];
void bfs(int s, int x, int t) {
int S = sqrt(n);
FOR(i, 0, n)
FOR(j, 0, S + 1) d[i][j] = 1e9;
d[s][x] = 0;
priority_queue<piii, vector<piii>, greater<piii>> pq;
pq.push({0, {s, x}});
while (!pq.empty()) {
piii tmp = pq.top();
pq.pop();
int w = tmp.X, s = tmp.Y.X, x = tmp.Y.Y;
// cout<<s<<" "<<x<<" "<<w<<"\n";
if (w != d[s][x]) continue;
if (x <= S) {
if (s - x >= 0 && minimize(d[s - x][x], w + 1)) pq.push({w + 1, {s - x, x}});
if (s + x < n && minimize(d[s + x][x], w + 1)) pq.push({w + 1, {s + x, x}});
}
for (auto u : v[s])
if (u <= S) {
if (minimize(d[s][u], w)) pq.push({w, {s, u}});
}
else {
int cnt = 0;
for (int t = s - u; t >= 0; t-=u) {
if (minimize(d[t][S + 1], w + cnt)) pq.push({d[t][S + 1], {t, S + 1}});
cnt++;
}
cnt = 0;
for (int t = s + u; t < n; t+=u) {
if (minimize(d[t][S + 1], w + cnt)) pq.push({d[t][S + 1], {t, S + 1}});
cnt++;
}
}
v[s].clear();
}
int res = 1e9;
FOR(j, 0, S + 1) minimize(res, d[t][j]);
cout<<(res == 1e9 ? -1 : res);
}
int main() {
// freopen("APIO15_SKYSCRAPER.inp", "r", stdin);
// freopen("APIO15_SKYSCRAPER.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
REP(i, 0, m) {
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
}
bfs(a[0], b[0], a[1]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2968 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2904 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2916 KB |
Output is correct |
2 |
Correct |
1 ms |
3080 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |