Problem p02644 (C++) — buggy submission, verdict: Runtime Error

/*

    author : s@if

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define NIL -1
#define fi first
#define sec second
#define MAX INT_MAX
#define ll long long
#define PI acos(-1.0)
#define MOD 1000000007
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define ull unsigned long long
#define For(i, a, b) for (int i = a; i <= (int)b; i++)
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

//*find_by_order(k)  gives the kth element;
// order_of_key(item)   gives the index(number of element strictly less than
// item) of item;

inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int Set(int N, int pos) { return N = N | (1 << pos); }

int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; // King's move
int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
int hx[] = {-2, -2, -1, +1, +2, +2, -1, +1}; // Knight's move
int hy[] = {+1, -1, +2, +2, -1, +1, -2, -2};
int dx[] = {+1, -1, +0, +0};
int dy[] = {+0, +0, +1, -1};

const int MAXN = (int)2e5 + 9;
const int INF = 1e9;

struct node {
  ll x, y, d;
  PLL w;
  node() {}
  node(int _x, int _y, int _d, PLL _w) { x = _x, y = _y, d = _d, w = _w; }

  bool operator<(const node &nd) const {
    return w.fi * nd.w.sec > w.sec * nd.w.fi;
  }
};

PLL ADD(PLL a, PLL b) {
  ll gc = __gcd(a.sec, b.sec);
  ll lc = (a.sec * b.sec) / gc;
  PLL ret;
  ret.sec = lc;
  ret.fi = lc / a.sec * a.fi + lc / b.sec * b.fi;

  return ret;
}

vector<string> grid;
vector<vector<vector<PLL>>> dist;

int main() {
  /*	freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
      ios_base::sync_with_stdio(false);
      cin.tie(0);
      cout.tie(0);  */

  ll i, j, k, l, m, n, p, q, x, y, u, v, r, tc, t;

  cin >> n >> m >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  dist.resize(n + 9);
  grid.resize(n + 9);
  for (i = 0; i < n; i++) {
    dist[i].resize(m + 9);
    for (j = 0; j < m; j++) {
      dist[i][j].resize(5);
      dist[i][j][0] = PLL(INF, 1);
      dist[i][j][1] = PLL(INF, 1);
      dist[i][j][2] = PLL(INF, 1);
      dist[i][j][3] = PLL(INF, 1);
    }
  }

  for (i = 0; i < n; i++) {
    cin >> grid[i];
  }

  priority_queue<node> pq;
  pq.push(node(x1, y1, 0, PLL(0, 1)));
  pq.push(node(x1, y1, 1, PLL(0, 1)));
  pq.push(node(x1, y1, 2, PLL(0, 1)));
  pq.push(node(x1, y1, 3, PLL(0, 1)));
  dist[x1][y1][0] = dist[x1][y1][1] = PLL(0, 1);
  dist[x1][y1][2] = dist[x1][y1][3] = PLL(0, 1);

  while (!pq.empty()) {
    node ndu = pq.top();
    pq.pop();

    for (i = 0; i < 4; i++) {
      x = ndu.x + dx[i];
      y = ndu.y + dy[i];
      if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '.') {
        PLL pw;
        if (ndu.d == i) {
          pw = ADD(ndu.w, PLL(1, k));
        } else {
          pw.fi = ndu.w.fi / ndu.w.sec;
          if (ndu.w.fi % ndu.w.sec != 0)
            pw.fi++;
          pw.sec = 1;
          pw = ADD(pw, PLL(1, k));
        }

        if (pw.fi * dist[x][y][i].sec < pw.sec * dist[x][y][i].fi) {
          dist[x][y][i] = pw;
          pq.push(node(x, y, i, pw));
        }
      }
    }
  }

  PLL ans = PLL(INF, 1);
  for (i = 0; i < 4; i++) {
    if (dist[x2][y2][i].fi * ans.sec < dist[x2][y2][i].sec * ans.fi)
      ans = dist[x2][y2][i];
  }

  if (ans.fi == INF)
    cout << -1 << endl;
  else {
    ll c = ans.fi / ans.sec + (ans.fi % ans.sec != 0);
    cout << c << endl;
  }
  //  main();

  return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)


Error: 0

Fixed submission:

/*

    author : s@if

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define NIL -1
#define fi first
#define sec second
#define MAX INT_MAX
#define INF 1e8
#define ll long long
#define PI acos(-1.0)
#define MOD 1000000007
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define ull unsigned long long
#define For(i, a, b) for (int i = a; i <= (int)b; i++)
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

//*find_by_order(k)  gives the kth element;
// order_of_key(item)   gives the index(number of element strictly less than
// item) of item;

inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int Set(int N, int pos) { return N = N | (1 << pos); }

int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; // King's move
int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
int hx[] = {-2, -2, -1, +1, +2, +2, -1, +1}; // Knight's move
int hy[] = {+1, -1, +2, +2, -1, +1, -2, -2};
int dx[] = {+1, -1, +0, +0};
int dy[] = {+0, +0, +1, -1};

const int MAXN = (int)2e5 + 9;

struct node {
  int x, y, d;
  double w;
  node() {}
  node(int _x, int _y, int _d, double _w) { x = _x, y = _y, d = _d, w = _w; }

  bool operator<(const node &nd) const { return w > nd.w; }
};

int main() {
  /*	freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
      ios_base::sync_with_stdio(false);
      cin.tie(0);
      cout.tie(0);  */

  int i, j, k, l, m, n, p, q, x, y, u, v, r, tc, t;

  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string grid[n + 9];
  double dist[n + 9][m + 9][4];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      dist[i][j][0] = INF;
      dist[i][j][1] = INF;
      dist[i][j][2] = INF;
      dist[i][j][3] = INF;
    }
  }

  for (i = 0; i < n; i++) {
    cin >> grid[i];
  }

  priority_queue<node> pq;
  pq.push(node(x1, y1, 0, 0));
  pq.push(node(x1, y1, 1, 0));
  pq.push(node(x1, y1, 2, 0));
  pq.push(node(x1, y1, 3, 0));
  dist[x1][y1][0] = dist[x1][y1][1] = 0;
  dist[x1][y1][2] = dist[x1][y1][3] = 0;

  while (!pq.empty()) {
    node ndu = pq.top();
    pq.pop();
    //  cout<<ndu.x<<" "<<ndu.y<<" "<<ndu.d<<" ";
    //  cout<<ndu.w.fi/ndu.w.sec+(ndu.w.fi%ndu.w.sec!=0)<<endl;

    for (i = 0; i < 4; i++) {
      x = ndu.x + dx[i];
      y = ndu.y + dy[i];
      if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '.') {
        // cout<<"go "<<x<<" "<<y<<" "<<ndu.x<<" "<<ndu.y<<" "<<ndu.d<<endl;
        double pw;
        if (ndu.d == i) {
          pw = ndu.w + 1.0 / k;
        } else {
          pw = ceil(ndu.w) + 1.0 / k;
        }

        // cout<<pw.fi<<" "<<pw.sec<<endl;
        if (pw < dist[x][y][i]) {
          // cout<<"push on pq\\n";
          dist[x][y][i] = pw;
          // dist[x][y][1] = pw;
          // dist[x][y][2] = pw;
          // dist[x][y][3] = pw;
          pq.push(node(x, y, i, pw));
          // pq.push(node(x,y,1,pw));
          // pq.push(node(x,y,2,pw));
          // pq.push(node(x,y,3,pw));
        }
      }
    }

    /* node ndv;
     if(ndu.d==0) ndv = node(ndu.x,ndu.y+1,0,ADD(ndu.w,PII(1,k)));
     else if(ndu.d==1) ndv = node(ndu.x-1,ndu.y,1,ADD(ndu.w,PII(1,k)));
     else if(ndu.d==2) ndv = node(ndu.x,ndu.y-1,2,ADD(ndu.w,PII(1,k)));
     else ndv = node(ndu.x+1,ndu.y,3,ADD(ndu.w,PII(1,k)));

     if(ndv.x>=0 && ndv.x<n && ndv.y>=0 && ndv.y<m){
         if(grid[ndv.x][ndv.y]=='.'){
             if(ndv.w.fi*dist[ndv.x][ndv.y][0].sec <
     ndv.w.sec*dist[ndv.x][ndv.y][0].fi){ dist[ndv.x][ndv.y][0] = ndv.w;
                 dist[ndv.x][ndv.y][1] = ndv.w;
                 dist[ndv.x][ndv.y][2] = ndv.w;
                 dist[ndv.x][ndv.y][3] = ndv.w;
                 pq.push(node(ndv.x,ndv.y,0,ndv.w));
                 pq.push(node(ndv.x,ndv.y,1,ndv.w));
                 pq.push(node(ndv.x,ndv.y,2,ndv.w));
                 pq.push(node(ndv.x,ndv.y,3,ndv.w));
             }
         }
     }*/
  }

  double ans = INF;
  for (i = 0; i < 4; i++) {
    if (dist[x2][y2][i] < ans)
      ans = dist[x2][y2][i];
  }

  if (ans == INF)
    cout << -1 << endl;
  else {
    int c = ceil(ans);
    cout << (int)c << endl;
  }
  //  main();

  return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)