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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;

const int inf = 1 << 18;

const int cmax = 250010;
int w, h;
ll d[cmax];
ll e[cmax];

struct state {
  int d, e;
  bool operator<(const state &arg) const { return (d - e) > (arg.d - arg.e); }
};

string table[510];

int n = 0;
state ex[cmax];

void dijkstra(char s, char o, ll *dist) {
  rep(i, cmax) dist[i] = inf;
  queue<pii> q;
  rep(i, h) rep(j, w) {
    if (table[i][j] == s) {
      int idx = w * i + j;
      dist[idx] = 0;
      pii in = mp(idx, 0);
      q.push(in);
    }
  }
  while (!q.empty()) {
    pii cur = q.front();
    q.pop();
    if (dist[cur.first] < cur.second)
      continue;
    int dir[4] = {-w, -1, 1, w};
    rep(i, 4) {
      pii next = cur;
      next.first += dir[i];
      next.second++;
      int cw = next.first % w;
      int ch = next.first / w;
      if (table[ch][cw] == '#' || table[ch][cw] == o)
        continue;
      if (dist[next.first] > next.second) {
        dist[next.first] = next.second;
        q.push(next);
      }
    }
  }
  return;
}

int main(void) {
  cin >> w >> h;
  rep(i, h) cin >> table[i];
  dijkstra('g', '*', d);
  dijkstra('*', '#', e);

  int sidx;
  ll dsum = 0;
  ll esum = 0;
  rep(i, h) rep(j, w) {
    int idx = i * w + j;
    if (table[i][j] == '.' || table[i][j] == 's') {
      if (d[idx] != inf) {
        ex[n].d = d[idx];
        ex[n++].e = e[idx];
      } else
        esum += e[idx];
    }
    if (table[i][j] == 's')
      sidx = idx;
  }
  sort(ex, ex + n);
  rep(i, n) dsum += ex[i].d;
  double ans = (1.0 * (dsum + esum) / n + e[sidx]);
  for (int i = n; i >= 1; --i) {
    ans = min(ans, 1.0 * (dsum + esum) / i + e[sidx]);
    int cur = n - i;
    dsum -= ex[cur].d;
    dsum += ex[cur].e;
  }
  if (d[sidx] != inf)
    ans = min(ans, 1.0 * d[sidx]);
  cout.precision(16);
  cout << fixed << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;

const int inf = 1 << 16;

const int cmax = 25010;
int w, h;
int d[cmax];
int e[cmax];

struct state {
  int d, e;
  bool operator<(const state &arg) const { return (d - e) > (arg.d - arg.e); }
};

string table[510];

int n = 0;
state ex[25010];

void dijkstra(char s, char o, int *dist) {
  rep(i, cmax) dist[i] = inf;
  queue<pii> q;
  rep(i, h) rep(j, w) {
    if (table[i][j] == s) {
      int idx = w * i + j;
      dist[idx] = 0;
      pii in = mp(idx, 0);
      q.push(in);
    }
  }
  while (!q.empty()) {
    pii cur = q.front();
    q.pop();
    if (dist[cur.first] < cur.second)
      continue;
    int dir[4] = {-w, -1, 1, w};
    rep(i, 4) {
      pii next = cur;
      next.first += dir[i];
      next.second++;
      int cw = next.first % w;
      int ch = next.first / w;
      if (table[ch][cw] == '#' || table[ch][cw] == o)
        continue;
      if (dist[next.first] > next.second) {
        dist[next.first] = next.second;
        q.push(next);
      }
    }
  }
  return;
}

void show(int *dist) {
  rep(i, h) {
    rep(j, w) {
      int idx = i * w + j;
      if (dist[idx] < 10)
        cout << dist[idx];
      else
        cout << '-';
    }
    cout << endl;
  }
}

int main(void) {
  cin >> w >> h;
  rep(i, h) cin >> table[i];
  dijkstra('g', '*', d);
  dijkstra('*', '#', e);

  int sidx;
  rep(i, h) rep(j, w) {
    int idx = i * w + j;
    if (table[i][j] == '.' || table[i][j] == 's') {
      ex[n].d = d[idx];
      ex[n++].e = e[idx];
    }
    if (table[i][j] == 's')
      sidx = idx;
  }
  sort(ex, ex + n);
  double ans = d[sidx];
  ll sum = 0;
  rep(i, n) sum += ex[i].d;
  for (int i = n; i >= 1; --i) {

    ans = min(ans, 1.0 * sum / i + e[sidx]);
    int cur = n - i;
    sum -= ex[cur].d;
    sum += ex[cur].e;
  }
  cout.precision(12);
  cout << fixed << ans << endl;
  show(d);
  return 0;
}