Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <string>
using namespace std;
const long long Maxn = 1000000 + 10, Maxm = 30000000 + 200;
const long long dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
const long long inf = (1ll << 60);
bool a[1010][1010];
bool vis[Maxn];
long long nxt[Maxm], to[Maxm];
long long len[Maxm], head[Maxn];
long long n, m, s, t;
long long edgecnt;
long long dis[Maxn];
struct node {
  long long pos, dis;
  node(long long x, long long y) { pos = x, dis = y; }
  bool operator<(const node &x) const { return x.dis < dis; }
};
inline void add(long long x, long long y, long long c) {
  ++edgecnt;
  nxt[edgecnt] = head[x];
  to[edgecnt] = y;
  len[edgecnt] = c;
  head[x] = edgecnt;
}
inline long long id(long long x, long long y) { return (x - 1) * m + y; }
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
void dij() {
  deque<int> q;
  for (long long i = 0; i <= (n * m) + 2; ++i)
    vis[i] = 0, dis[i] = inf;
  vis[s] = 1, dis[s] = 0ll, q.push_back(s);
  while (q.size()) {
    long long x = q.front();
    q.pop_front();
    vis[x] = 0;
    for (long long i = head[x]; i; i = nxt[i]) {
      long long y = to[i];
      if (dis[y] > dis[x] + len[i]) {
        dis[y] = (long long)dis[x] + len[i];
        if (!vis[y]) {
          vis[y] = 1;
          if (!len[i])
            q.push_front(y);
          else
            q.push_back(y);
        }
      }
    }
  }
}
inline bool check(long long x, long long y) {
  return (x < 1 || y < 1 || x > n || y > m || a[x][y]);
}
int main() {
  n = read(), m = read();
  long long x = read(), y = read();
  s = id(x, y);
  x = read(), y = read();
  t = id(x, y);
  for (long long i = 1; i <= n; ++i) {
    string str;
    cin >> str;
    for (long long j = 1; j <= m; ++j)
      if (str[j - 1] == '#')
        a[i][j] = 1;
  }
  for (long long i = 1; i <= n; ++i)
    for (long long j = 1; j <= m; ++j) {
      if (a[i][j])
        continue;
      for (long long x = i - 2; x <= i + 2; ++x)
        for (long long y = j - 2; y <= j + 2; ++y) {
          if (check(x, y) || (i == x && j == y))
            continue;
          add(id(i, j), id(x, y), 1);
        }
      for (long long k = 0; k < 4; ++k) {
        long long x = i + dx[k], y = j + dy[k];
        if (check(x, y))
          continue;
        add(id(i, j), id(x, y), 0);
      }
    }
  dij();
  if (dis[t] == inf)
    puts("-1");
  else
    printf("%lld\\n", dis[t]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
const long long Maxn = 1000000 + 10, Maxm = 30000000 + 200;
const long long dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
const long long inf = (1ll << 60);
bool a[1010][1010];
bool vis[Maxn];
long long nxt[Maxm], to[Maxm];
long long len[Maxm], head[Maxn];
long long n, m, s, t;
long long edgecnt;
long long dis[Maxn];
struct node {
  long long pos, dis;
  node(long long x, long long y) { pos = x, dis = y; }
  bool operator<(const node &x) const { return x.dis < dis; }
};
inline void add(long long x, long long y, long long c) {
  ++edgecnt;
  nxt[edgecnt] = head[x];
  to[edgecnt] = y;
  len[edgecnt] = c;
  head[x] = edgecnt;
}
inline long long id(long long x, long long y) { return (x - 1) * m + y; }
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
void dij() {
  queue<int> q;
  for (long long i = 0; i <= (n * m) + 2; ++i)
    vis[i] = 0, dis[i] = inf;
  vis[s] = 1, dis[s] = 0ll, q.push(s);
  while (q.size()) {
    long long x = q.front();
    q.pop();
    vis[x] = 0;
    for (long long i = head[x]; i; i = nxt[i]) {
      long long y = to[i];
      if (dis[y] > dis[x] + len[i]) {
        dis[y] = (long long)dis[x] + len[i];
        if (!vis[y])
          vis[y] = 1, q.push(y);
      }
    }
  }
}
inline bool check(long long x, long long y) {
  return (x < 1 || y < 1 || x > n || y > m || a[x][y]);
}
int main() {
  n = read(), m = read();
  long long x = read(), y = read();
  s = id(x, y);
  x = read(), y = read();
  t = id(x, y);
  for (long long i = 1; i <= n; ++i) {
    string str;
    cin >> str;
    for (long long j = 1; j <= m; ++j)
      if (str[j - 1] == '#')
        a[i][j] = 1;
  }
  for (long long i = 1; i <= n; ++i)
    for (long long j = 1; j <= m; ++j) {
      if (a[i][j])
        continue;
      for (long long x = i - 2; x <= i + 2; ++x)
        for (long long y = j - 2; y <= j + 2; ++y) {
          if (check(x, y) || (i == x && j == y))
            continue;
          add(id(i, j), id(x, y), 1);
        }
      for (long long k = 0; k < 4; ++k) {
        long long x = i + dx[k], y = j + dy[k];
        if (check(x, y))
          continue;
        add(id(i, j), id(x, y), 0);
      }
    }
  dij();
  if (dis[t] == inf)
    puts("-1");
  else
    printf("%lld\\n", dis[t]);
  return 0;
}