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

#include <bits/stdc++.h>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))
#define PB push_back
#define mkp make_pair
#define F first
#define S second
#define INF 0x3f3f3f3f
#define EPS 1e-8

typedef vector<int> vint;
typedef pair<int, int> pii;

int main() {
  int H, W;
  cin >> W >> H;
  vector<string> v(H);
  rep(i, H) cin >> v[i];
  vector<vint> vv1(H, vint(W, INF));
  vector<vint> vv2(H, vint(W, INF));
  pii s;
  queue<pii> q1, q2;
  long double p = 0;
  rep(i, H) {
    rep(j, W) {
      if (v[i][j] == 'g') {
        q1.push(pii(i, j));
      } else if (v[i][j] == 's') {
        s = pii(i, j);
        v[i][j] = '.';
        p += 1.0;
      } else if (v[i][j] == '*') {
        q2.push(pii(i, j));
      } else if (v[i][j] == '.') {
        p += 1.0;
      }
    }
  }
  p = 1 / p;
  int cnt = 0;
  int dd[] = {0, 1, 0, -1, 0};
  while (q1.size()) {
    int qs = q1.size();
    rep(i, qs) {
      pii top = q1.front();
      q1.pop();
      if (vv1[top.F][top.S] != INF)
        continue;
      vv1[top.F][top.S] = cnt;
      rep(j, 4) {
        pii nex = top;
        nex.F += dd[j];
        nex.S += dd[j + 1];
        if (v[nex.F][nex.S] == '#')
          continue;
        if (v[nex.F][nex.S] == '*')
          continue;
        q1.push(nex);
      }
    }
    cnt++;
  }
  cnt = 0;
  while (q2.size()) {
    int qs = q2.size();
    rep(i, qs) {
      pii top = q2.front();
      q2.pop();
      if (vv2[top.F][top.S] != INF)
        continue;
      vv2[top.F][top.S] = cnt;
      rep(j, 4) {
        pii nex = top;
        nex.F += dd[j];
        nex.S += dd[j + 1];
        if (v[nex.F][nex.S] == '#')
          continue;
        if (v[nex.F][nex.S] == '*')
          continue;
        q2.push(nex);
      }
    }
    cnt++;
  }
  int maxn = 0;
  rep(i, H) rep(j, W) {
    if (vv1[i][j] != INF)
      maxn = max(maxn, vv1[i][j]);
  }
  map<pair<int, int>, int> ma;
  double bb = 0;
  rep(i, H) {
    rep(j, W) {
      if (v[i][j] != '.')
        continue;
      int x = vv1[i][j];
      int y = vv2[i][j];
      int t = min(x, y);
      bb += t * p;
      x -= t;
      y -= t;
      pii z = pii(x, y);
      if (ma.count(z) == 0)
        ma[z] = 0;
      ma[z] += 1;
    }
  }
  long double ans = 1e100;
  rep(o, maxn + 1) {
    long double a = 0;
    long double b = bb;
    for (auto x : ma) {
      if (x.F.F - x.F.S <= o) {
        b += p * x.F.F * x.S;
      } else {
        b += p * x.F.S * x.S;
        a += p * x.S;
      }
    }
    long double e = b / (1 - a);
    long double tmp;
    if (vv1[s.F][s.S] - vv2[s.F][s.S] <= o)
      tmp = vv1[s.F][s.S];
    else
      tmp = vv2[s.F][s.S] + e;
    ans = min(ans, tmp);
  }
  cout << fixed << setprecision(20);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))
#define PB push_back
#define mkp make_pair
#define F first
#define S second
#define INF 0x3f3f3f3f
#define EPS 1e-8

typedef vector<int> vint;
typedef pair<int, int> pii;

int main() {
  int H, W;
  cin >> W >> H;
  vector<string> v(H);
  rep(i, H) cin >> v[i];
  vector<vint> vv1(H, vint(W, INF));
  vector<vint> vv2(H, vint(W, INF));
  pii s;
  queue<pii> q1, q2;
  long double p = 0;
  rep(i, H) {
    rep(j, W) {
      if (v[i][j] == 'g') {
        q1.push(pii(i, j));
      } else if (v[i][j] == 's') {
        s = pii(i, j);
        v[i][j] = '.';
        p += 1.0;
      } else if (v[i][j] == '*') {
        q2.push(pii(i, j));
      } else if (v[i][j] == '.') {
        p += 1.0;
      }
    }
  }
  p = 1 / p;
  int cnt = 0;
  int dd[] = {0, 1, 0, -1, 0};
  while (q1.size()) {
    int qs = q1.size();
    rep(i, qs) {
      pii top = q1.front();
      q1.pop();
      if (vv1[top.F][top.S] != INF)
        continue;
      vv1[top.F][top.S] = cnt;
      rep(j, 4) {
        pii nex = top;
        nex.F += dd[j];
        nex.S += dd[j + 1];
        if (v[nex.F][nex.S] == '#')
          continue;
        if (v[nex.F][nex.S] == '*')
          continue;
        q1.push(nex);
      }
    }
    cnt++;
  }
  cnt = 0;
  while (q2.size()) {
    int qs = q2.size();
    rep(i, qs) {
      pii top = q2.front();
      q2.pop();
      if (vv2[top.F][top.S] != INF)
        continue;
      vv2[top.F][top.S] = cnt;
      rep(j, 4) {
        pii nex = top;
        nex.F += dd[j];
        nex.S += dd[j + 1];
        if (v[nex.F][nex.S] == '#')
          continue;
        if (v[nex.F][nex.S] == '*')
          continue;
        q2.push(nex);
      }
    }
    cnt++;
  }
  int maxn = 0;
  rep(i, H) rep(j, W) {
    if (vv1[i][j] != INF)
      maxn = max(maxn, vv1[i][j]);
    if (vv2[i][j] != INF)
      maxn = max(maxn, vv2[i][j]);
  }
  map<pair<int, int>, int> ma;
  double bb = 0;
  rep(i, H) {
    rep(j, W) {
      if (v[i][j] != '.')
        continue;
      int x = vv1[i][j];
      int y = vv2[i][j];
      int t = min(x, y);
      bb += t * p;
      x -= t;
      y -= t;
      pii z = pii(x, y);
      if (ma.count(z) == 0)
        ma[z] = 0;
      ma[z] += 1;
    }
  }
  long double ans = 1e100;
  rep(o, maxn + 1) {
    long double a = 0;
    long double b = bb;
    for (auto x : ma) {
      if (x.F.F - x.F.S <= o) {
        b += p * x.F.F * x.S;
      } else {
        b += p * x.F.S * x.S;
        a += p * x.S;
      }
    }
    long double e = b / (1 - a);
    long double tmp;
    if (vv1[s.F][s.S] - vv2[s.F][s.S] <= o)
      tmp = vv1[s.F][s.S];
    else
      tmp = vv2[s.F][s.S] + e;
    ans = min(ans, tmp);
  }
  cout << fixed << setprecision(20);
  cout << ans << endl;
}