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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 21474836001234567
#define double long double

int main() {
  int w, h;
  cin >> w >> h;
  vector<string> vec(h);
  rep(i, h) cin >> vec[i];

  int sx, sy, tx, ty;
  vector<int> spx, spy;
  rep(i, h) rep(j, w) {
    if (vec[i][j] == 's')
      sx = i, sy = j;
    if (vec[i][j] == 'g')
      tx = i, ty = j;
    if (vec[i][j] == '*')
      spx.pb(i), spy.pb(j);
  }

  vector<vector<long>> d(h, vector<long>(w, INF));
  // g?????§??????????????¢
  {
    queue<int> xs, ys;
    d[tx][ty] = 0;
    xs.push(tx);
    ys.push(ty);
    while (!xs.empty()) {
      int x = xs.front();
      xs.pop();
      int y = ys.front();
      ys.pop();
      const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (vec[nx][ny] != '#' && vec[nx][ny] != '*' &&
            d[nx][ny] > d[x][y] + 1) {
          d[nx][ny] = d[x][y] + 1;
          xs.push(nx);
          ys.push(ny);
        }
      }
    }
  }

  vector<vector<long>> ds(h, vector<long>(w, INF));
  // ????????????spring?????§??????????????¢
  {
    queue<int> xs, ys;
    rep(i, spx.size()) {
      xs.push(spx[i]);
      ys.push(spy[i]);
      ds[spx[i]][spy[i]] = 0;
    }
    while (!xs.empty()) {
      int x = xs.front();
      xs.pop();
      int y = ys.front();
      ys.pop();
      const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (vec[nx][ny] != '#' && vec[nx][ny] != '*' &&
            ds[nx][ny] > ds[x][y] + 1) {
          ds[nx][ny] = ds[x][y] + 1;
          xs.push(nx);
          ys.push(ny);
        }
      }
    }
  }

  double p =
      INF; // ????????§?£???°??????????????¨?????????????§????????????????
  {
    int cnt = 1;
    rep(i, h) rep(j, w) if (vec[i][j] == '.') cnt++;

    double l = 0, r = 500.0 * 500 * 500 * 500;
    rep(_, 1000) {
      double m = (l + r) / 2.0;
      double accm = min<double>(d[sx][sy], ds[sx][sy] + m);
      rep(i, h) rep(j, w) if (vec[i][j] == '.') {
        accm += min<double>(d[i][j], ds[i][j] + m);
      }
      p = accm / cnt;
      if (p < m)
        r = m;
      else
        l = m;
    }
    p = (r + l) / 2.0;
  }

  printf("%.10llf\\n", min<double>(d[sx][sy], ds[sx][sy] + p));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 21474836001234567

int main() {
  int w, h;
  cin >> w >> h;
  vector<string> vec(h);
  rep(i, h) cin >> vec[i];

  int sx, sy, tx, ty;
  vector<int> spx, spy;
  rep(i, h) rep(j, w) {
    if (vec[i][j] == 's')
      sx = i, sy = j;
    if (vec[i][j] == 'g')
      tx = i, ty = j;
    if (vec[i][j] == '*')
      spx.pb(i), spy.pb(j);
  }

  vector<vector<long>> d(h, vector<long>(w, INF));
  // g?????§??????????????¢
  {
    queue<int> xs, ys;
    d[tx][ty] = 0;
    xs.push(tx);
    ys.push(ty);
    while (!xs.empty()) {
      int x = xs.front();
      xs.pop();
      int y = ys.front();
      ys.pop();
      const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (vec[nx][ny] != '#' && vec[nx][ny] != '*' &&
            d[nx][ny] > d[x][y] + 1) {
          d[nx][ny] = d[x][y] + 1;
          xs.push(nx);
          ys.push(ny);
        }
      }
    }
  }

  vector<vector<long>> ds(h, vector<long>(w, INF));
  // ????????????spring?????§??????????????¢
  {
    queue<int> xs, ys;
    rep(i, spx.size()) {
      xs.push(spx[i]);
      ys.push(spy[i]);
      ds[spx[i]][spy[i]] = 0;
    }
    while (!xs.empty()) {
      int x = xs.front();
      xs.pop();
      int y = ys.front();
      ys.pop();
      const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (vec[nx][ny] != '#' && vec[nx][ny] != '*' &&
            ds[nx][ny] > ds[x][y] + 1) {
          ds[nx][ny] = ds[x][y] + 1;
          xs.push(nx);
          ys.push(ny);
        }
      }
    }
  }

  double p =
      INF; // ????????§?£???°??????????????¨?????????????§????????????????
  {
    int cnt = 1;
    rep(i, h) rep(j, w) if (vec[i][j] == '.') cnt++;

    double l = 0, r = 500.0 * 500 * 500 * 500;
    while ((r - l) >= 1e-10) {
      double m = (l + r) / 2.0;
      double accm = min<double>(d[sx][sy], ds[sx][sy] + m);
      rep(i, h) rep(j, w) if (vec[i][j] == '.') {
        accm += min<double>(d[i][j], ds[i][j] + m);
      }
      p = accm / cnt;
      if (p < m)
        r = m;
      else
        l = m;
    }
    p = (r + l) / 2.0;
  }

  printf("%.10f\\n", min<double>(d[sx][sy], ds[sx][sy] + p));

  return 0;
}