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

#include <bits/stdc++.h>

using namespace std;

using dbl = long double;
using Pi = pair<int, int>;

const dbl eps = 1e-15;
#define lt(a, b) ((a) - (b) < -eps)
#define eq(a, b) (fabs((a) - (b)) < eps)

int W, H;
char mas[505][505];
Pi S, G;
vector<Pi> Bs;

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

bool in(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

vector<vector<dbl>> bfs(const vector<Pi> &s) {
  // cout<<"!!!!!"<<endl;
  queue<Pi> que;
  vector<vector<dbl>> dist(H, vector<dbl>(W, -1));
  for (Pi p : s) {
    que.emplace(p);
    dist[p.first][p.second] = 0;
  }
  while (!que.empty()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    // cout<<mas[y][x]<<" "<<y<<" "<<x<<endl;
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!in(ny, nx) || mas[ny][nx] == '#' || mas[ny][nx] == '*')
        continue;
      if (dist[ny][nx] == -1) {
        dist[ny][nx] = dist[y][x] + 1;
        que.emplace(ny, nx);
      }
    }
  }
  return dist;
}

vector<vector<dbl>> db;
vector<vector<dbl>> dg;

bool check(dbl mb) {
  dbl sum = 0;
  int num = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (mas[i][j] == '.') {
        if (dg[i][j] == -1 && db[i][j] == -1)
          continue; // assert(false);
        else if (dg[i][j] == -1)
          sum += db[i][j] + mb;
        else if (db[i][j] == -1)
          sum += dg[i][j];
        else
          sum += min(dg[i][j], db[i][j] + mb);
        ++num;
      }
    }
  }
  assert(num > 0);
  sum /= num;
  return sum >= mb;
}

int main() {
  cin >> W >> H;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> mas[i][j];
      if (mas[i][j] == 's')
        S = Pi(i, j), mas[i][j] = '.';
      else if (mas[i][j] == 'g')
        G = Pi(i, j);
      else if (mas[i][j] == '*')
        Bs.emplace_back(i, j);
    }
  }

  db = bfs(Bs);
  dg = bfs({G});

  dbl ans = dg[S.first][S.second];
  // cout<<ans<<endl;
  /*
  bool flag = false;
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if(mas[i][j] == '.' && dg[i][j] == -1 && db[i][j] == -1) flag = true;
    }
  }
  */
  if (ans == -1)
    ans = DBL_MAX / 2;
  else if (db[S.first][S.second] == -1) {
    // else if(flag) {
    cout << fixed << setprecision(12) << ans << endl;
    return 0;
  }

  dbl lb = 0, ub = 1e20;
  for (int i = 0; i < 200; ++i) {
    dbl mb = (lb + ub) / 2;
    if (check(mb))
      lb = mb;
    else
      ub = mb;
  }

  ans = min(ans, db[S.first][S.second] + lb);

  cout << fixed << setprecision(12) << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using dbl = long double;
using Pi = pair<int, int>;

const dbl eps = 1e-15;
#define lt(a, b) ((a) - (b) < -eps)
#define eq(a, b) (fabs((a) - (b)) < eps)

int W, H;
char mas[505][505];
Pi S, G;
vector<Pi> Bs;

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

bool in(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

vector<vector<dbl>> bfs(const vector<Pi> &s) {
  // cout<<"!!!!!"<<endl;
  queue<Pi> que;
  vector<vector<dbl>> dist(H, vector<dbl>(W, -1));
  for (Pi p : s) {
    que.emplace(p);
    dist[p.first][p.second] = 0;
  }
  while (!que.empty()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    // cout<<mas[y][x]<<" "<<y<<" "<<x<<endl;
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!in(ny, nx) || mas[ny][nx] == '#' || mas[ny][nx] == '*')
        continue;
      if (dist[ny][nx] == -1) {
        dist[ny][nx] = dist[y][x] + 1;
        que.emplace(ny, nx);
      }
    }
  }
  return dist;
}

vector<vector<dbl>> db;
vector<vector<dbl>> dg;

bool check(dbl mb) {
  dbl sum = 0;
  int num = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (mas[i][j] == '.') {
        if (dg[i][j] == -1 && db[i][j] == -1)
          assert(false);
        else if (dg[i][j] == -1)
          sum += db[i][j] + mb;
        else if (db[i][j] == -1)
          sum += dg[i][j];
        else
          sum += min(dg[i][j], db[i][j] + mb);
        ++num;
      }
    }
  }
  sum /= num;
  return sum >= mb;
}

int main() {
  cin >> W >> H;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> mas[i][j];
      if (mas[i][j] == 's')
        S = Pi(i, j), mas[i][j] = '.';
      else if (mas[i][j] == 'g')
        G = Pi(i, j);
      else if (mas[i][j] == '*')
        Bs.emplace_back(i, j);
    }
  }

  db = bfs(Bs);
  dg = bfs({G});

  dbl ans = dg[S.first][S.second];
  // cout<<ans<<endl;
  if (ans == -1)
    ans = DBL_MAX / 2;
  else if (db[S.first][S.second] == -1) {
    cout << fixed << setprecision(12) << ans << endl;
    return 0;
  }

  dbl lb = 0, ub = 1e20;
  for (int i = 0; i < 200; ++i) {
    dbl mb = (lb + ub) / 2;
    if (check(mb))
      lb = mb;
    else
      ub = mb;
  }

  ans = min(ans, db[S.first][S.second] + lb);

  cout << fixed << setprecision(12) << ans << endl;

  return 0;
}