Submission #393941

#TimeUsernameProblemLanguageResultExecution timeMemory
393941gispisquaredJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1088 ms63116 KiB
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define F first
#define S second
typedef pair<int, int> pi;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;
  int B[M], P[M];
  vector<int> dists[N];
  map<int, int> m[N];
  vector<bool> vis(N);
  for (int i = 0; i < M; ++i) {
    cin >> B[i] >> P[i];
    dists[B[i]].PB(P[i]);
  }
  queue<pi> bfs({{B[0], P[0]}});
  m[B[0]][P[0]] = 1;
  while (!bfs.empty() && bfs.front().F != B[1]) {
    auto tmp = bfs.front();
    bfs.pop();
    if (tmp.F + tmp.S < N &&
        m[tmp.F + tmp.S].find(tmp.S) == m[tmp.F + tmp.S].end()) {
      m[tmp.F + tmp.S][tmp.S] = m[tmp.F][tmp.S] + 1;
      bfs.push({tmp.F + tmp.S, tmp.S});
    }
    if (tmp.F - tmp.S >= 0 &&
        m[tmp.F - tmp.S].find(tmp.S) == m[tmp.F - tmp.S].end()) {
      m[tmp.F - tmp.S][tmp.S] = m[tmp.F][tmp.S] + 1;
      bfs.push({tmp.F - tmp.S, tmp.S});
    }
    if (!vis[tmp.F])
      for (auto i : dists[tmp.F]) {
        m[tmp.F][i] = m[tmp.F][tmp.S];
        if (tmp.F + i < N && m[tmp.F + i].find(i) == m[tmp.F + i].end()) {
          m[tmp.F + i][i] = m[tmp.F][i] + 1;
          bfs.push({tmp.F + i, i});
        }
        if (tmp.F - i >= 0 && m[tmp.F - i].find(i) == m[tmp.F - i].end()) {
          m[tmp.F - i][i] = m[tmp.F][i] + 1;
          bfs.push({tmp.F - i, i});
        }
      }
    vis[tmp.F] = 1;
  }
  if (bfs.front().F == B[1])
    cout << m[B[1]][bfs.front().S] - 1;
  else
    cout << -1;
}
#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...