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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int H, W, K, x1, x2, Y1, y2;

string c[1000006];

string tmp;

vector<vector<vector<long long int>>> dis, check;

long long int INF = 9999999999999999;

set<pair<long long int, pair<pair<int, int>, int>>> S;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

priority_queue<pair<long long int, pair<pair<int, int>, int>>> PQ;

int main(void) {

  cin >> H >> W >> K;

  cin >> x1 >> Y1 >> x2 >> y2;

  for (int i = 0; i <= W + 1; i++) {
    c[0] += "@";
    c[H + 1] += "@";
  }
  for (int i = 1; i <= H; i++) {
    cin >> tmp;
    c[i] = "@" + tmp + "@";
  }

  dis.resize(H + 2);
  check.resize(H + 2);
  for (int i = 0; i <= H + 1; i++) {
    dis[i].resize(W + 2);
    check[i].resize(W + 2);
    for (int j = 0; j <= W + 1; j++) {
      dis[i][j].resize(4);
      check[i][j].resize(4);
    }
  }

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      for (int k = 0; k < 4; k++) {
        dis[i][j][k] = INF;
        if (i == x1 && j == Y1) {
          dis[i][j][k] = 0;
        }
        if (c[i][j] != '@') {
          // S.insert(make_pair(dis[i][j][k], make_pair(make_pair(i, j), k)));
          PQ.push(make_pair(-dis[i][j][k], make_pair(make_pair(i, j), k)));
        }
      }
    }
  }

  /*	while (S.size() > 0) {
                  auto it = S.begin();

                  long long int tmp_dis = (*it).first;
                  int x = (*it).second.first.first;
                  int y = (*it).second.first.second;
                  int k = (*it).second.second;

                  if (tmp_dis != dis[x][y][k]) { continue; }
                  dis[x][y][k] = (*it).first;
                  check[x][y][k] = 1;

                  //cout << "x= " << x << " y= " <<y << " k= " << k << " dis=
     "<<dis[x][y][k]<<endl;

                  S.erase(it);

                  for (int u = 0; u < 4; u++) {

                          long long int ndis;
                          int nx, ny;
                          if (u == k) {
                                  ndis = dis[x][y][k] + 1;
                                  nx = x+dx[k];
                                  ny = y+dy[k];
                          }
                          else {
                                  ndis = ((dis[x][y][k]+ K - 1) / K)*K;
                                  nx = x;
                                  ny = y;
                          }
                          pair<long long int, pair<pair<int, int>, int> > tmp;
                          tmp.first = dis[nx][ny][u];
                          tmp.second.first.first = nx;
                          tmp.second.first.second = ny;
                          tmp.second.second = u;
                          S.erase(tmp);
                          tmp.first = ndis; //cout << "ndis= " << ndis << endl;
                          if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
                                  //cout << "insert" << endl;
                                  dis[nx][ny][u] = ndis;  S.insert(tmp);
                          }
                  }
          }
  */
  while (PQ.size() > 0) {

    auto t = PQ.top();

    long long int tmp_dis = -t.first;
    int x = t.second.first.first;
    int y = t.second.first.second;
    int k = t.second.second;

    // cout << "x= " << x << " y= " << y << " k= " << k << " dis= " << tmp_dis
    // << endl;

    PQ.pop();
    if (tmp_dis > dis[x][y][k]) {
      continue;
    }
    dis[x][y][k] = -t.first;
    check[x][y][k] = 1;

    for (int u = 0; u < 4; u++) {

      long long int ndis;
      int nx, ny;
      if (u == k) {
        ndis = dis[x][y][k] + 1;
        nx = x + dx[k];
        ny = y + dy[k];
      } else {
        ndis = ((dis[x][y][k] + K - 1) / K) * K;
        nx = x;
        ny = y;
      }
      pair<long long int, pair<pair<int, int>, int>> tmp;
      tmp.first = -dis[nx][ny][u];
      tmp.second.first.first = nx;
      tmp.second.first.second = ny;
      tmp.second.second = u;
      // S.erase(tmp);
      tmp.first = -ndis; // cout << "ndis= " << ndis << endl;
      if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
        // cout << "insert" << endl;
        if (dis[nx][ny][u] > ndis) {
          dis[nx][ny][u] = ndis; // S.insert(tmp);
          PQ.push(tmp);
        }
      }
    }
  }

  long long int ans = INF;

  for (int k = 0; k < 4; k++) {
    ans = min(ans, dis[x2][y2][k]);
  }

  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ((ans + K - 1) / K) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int H, W, K, x1, x2, Y1, y2;

string c[1000006];

string tmp;

vector<vector<vector<long long int>>> dis, check;

long long int INF = 9999999999999999;

set<pair<long long int, pair<pair<int, int>, int>>> S;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

priority_queue<pair<long long int, pair<pair<int, int>, int>>> PQ;

int main(void) {

  cin >> H >> W >> K;

  cin >> x1 >> Y1 >> x2 >> y2;

  for (int i = 0; i <= W + 1; i++) {
    c[0] += "@";
    c[H + 1] += "@";
  }
  for (int i = 1; i <= H; i++) {
    cin >> tmp;
    c[i] = "@" + tmp + "@";
  }

  dis.resize(H + 2);
  check.resize(H + 2);
  for (int i = 0; i <= H + 1; i++) {
    dis[i].resize(W + 2);
    check[i].resize(W + 2);
    for (int j = 0; j <= W + 1; j++) {
      dis[i][j].resize(4);
      check[i][j].resize(4);
    }
  }

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      for (int k = 0; k < 4; k++) {
        dis[i][j][k] = INF;
        if (i == x1 && j == Y1) {
          dis[i][j][k] = 0;
        }
        if (c[i][j] != '@') {
          // S.insert(make_pair(dis[i][j][k], make_pair(make_pair(i, j), k)));
          PQ.push(make_pair(-dis[i][j][k], make_pair(make_pair(i, j), k)));
        }
      }
    }
  }

  /*	while (S.size() > 0) {
                  auto it = S.begin();

                  long long int tmp_dis = (*it).first;
                  int x = (*it).second.first.first;
                  int y = (*it).second.first.second;
                  int k = (*it).second.second;

                  if (tmp_dis != dis[x][y][k]) { continue; }
                  dis[x][y][k] = (*it).first;
                  check[x][y][k] = 1;

                  //cout << "x= " << x << " y= " <<y << " k= " << k << " dis=
     "<<dis[x][y][k]<<endl;

                  S.erase(it);

                  for (int u = 0; u < 4; u++) {

                          long long int ndis;
                          int nx, ny;
                          if (u == k) {
                                  ndis = dis[x][y][k] + 1;
                                  nx = x+dx[k];
                                  ny = y+dy[k];
                          }
                          else {
                                  ndis = ((dis[x][y][k]+ K - 1) / K)*K;
                                  nx = x;
                                  ny = y;
                          }
                          pair<long long int, pair<pair<int, int>, int> > tmp;
                          tmp.first = dis[nx][ny][u];
                          tmp.second.first.first = nx;
                          tmp.second.first.second = ny;
                          tmp.second.second = u;
                          S.erase(tmp);
                          tmp.first = ndis; //cout << "ndis= " << ndis << endl;
                          if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
                                  //cout << "insert" << endl;
                                  dis[nx][ny][u] = ndis;  S.insert(tmp);
                          }
                  }
          }
  */
  while (PQ.size() > 0) {

    auto t = PQ.top();

    long long int tmp_dis = -t.first;
    int x = t.second.first.first;
    int y = t.second.first.second;
    int k = t.second.second;

    // cout << "x= " << x << " y= " << y << " k= " << k << " dis= " << tmp_dis
    // << endl;

    PQ.pop();
    if (tmp_dis > dis[x][y][k]) {
      continue;
    }
    dis[x][y][k] = -t.first;
    check[x][y][k] = 1;

    for (int u = 0; u < 4; u++) {

      long long int ndis;
      int nx, ny;
      if (u == k) {
        ndis = dis[x][y][k] + 1;
        nx = x + dx[k];
        ny = y + dy[k];
      } else {
        ndis = ((dis[x][y][k] + K - 1) / K) * K;
        nx = x;
        ny = y;
      }
      pair<long long int, pair<pair<int, int>, int>> tmp;
      tmp.first = -dis[nx][ny][u];
      tmp.second.first.first = nx;
      tmp.second.first.second = ny;
      tmp.second.second = u;
      // S.erase(tmp);
      tmp.first = -ndis; // cout << "ndis= " << ndis << endl;
      if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
        // cout << "insert" << endl;
        dis[nx][ny][u] = ndis; // S.insert(tmp);
        PQ.push(tmp);
      }
    }
  }

  long long int ans = INF;

  for (int k = 0; k < 4; k++) {
    ans = min(ans, dis[x2][y2][k]);
  }

  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ((ans + K - 1) / K) << endl;
  }

  return 0;
}