제출 #725326

#제출 시각아이디문제언어결과실행 시간메모리
725326quiet_spaceJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
313 ms112204 KiB
#include <bits/stdc++.h>
#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;
}
using tiii = std::tuple <int, int, int>;
const int maxn = 30005;
std::bitset <maxn> b[maxn];
std::vector <int> v[maxn];
std::queue <tiii> q;
int f[maxn];
int main (void) {
  int n = read(), m = read(), pos0, pos1;
  pos0 = read();
  v[pos0].push_back (read());
  pos1 = read();
  v[pos1].push_back (read());
  FOR(i,3,m) {
    int x = read(), y = read();
    v[x].push_back (y);
  }
  f[pos0] = 1;
  for(auto&it:v[pos0]) q.emplace (pos0, it, 1);
  while(!f[pos1] && !q.empty()) {
    int x, y, z; std::tie (x, y, z) = q.front(); q.pop();
    if(x + y < n && !b[x + y].test (y)) {
      b[x + y].set (y); q.emplace (x + y, y, z + 1);
      if(!f[x + y]) {
        f[x + y] = z + 1;
        for(auto&it:v[x + y]) q.emplace (x + y, it, z + 1);
      }
    }
    if(x - y >= 0 && !b[x - y].test (y)) {
      b[x - y].set (y); q.emplace (x - y, y, z + 1);
      if(!f[x - y]) {
        f[x - y] = z + 1;
        for(auto&it:v[x - y]) q.emplace (x - y, it, z + 1);
      }
    }
  }
  printf("%d\n", f[pos1] - 1);
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...