This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(int i=j; i<=k; ++i)
#define ROF(i,j,k) for(int i=j; i>=k; --i)
inline int read (void) {
int x = 0, f = 1, ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
const int maxn = 30005;
struct Node {
int pos, fot, dis;
};
bitset <maxn> b[maxn];
vector <int> v[maxn];
queue <Node> q;
int d[maxn];
int main (void) {
int n = read(), m = read(), p0 = -1, p1 = -1;
FOR(i,1,m) {
int x = read(), y = read();
if(p0 == -1) p0 = x;
else if(p1 == -1) p1 = x;
v[x].push_back(y);
}
d[p0] = 1;
for(auto&it:v[p0])
if(!b[p0].test(it)) {
b[p0].set(it);
q.push((Node) {p0, it, 1});
}
while(!d[p1] && !q.empty()) {
Node cur = q.front(); q.pop();
int x = cur.pos, y = cur.fot, z = cur.dis;
if(x - y >= 0 && !b[x-y].test(y)) {
b[x-y].set(y);
q.push((Node) {x-y, y, z+1});
if(!d[x-y]) {
d[x-y] = z+1;
for(auto&it:v[x-y])
if(!b[x-y].test(it)) {
b[x-y].set(it);
q.push((Node) {x-y, it, z+1});
}
}
}
if(x + y < n && !b[x+y].test(y)) {
b[x+y].set(y);
q.push((Node) {x+y, y, z+1});
if(!d[x+y]) {
d[x+y] = z+1;
for(auto&it:v[x+y])
if(!b[x+y].test(it)) {
b[x+y].set(it);
q.push((Node) {x+y, it, z+1});
}
}
}
}
printf("%d\n", d[p1]-1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |