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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Point {
  int x, y, cost;
  Point() { ; }
  Point(int x, int y, int cost) : x(x), y(y), cost(cost) { ; }
};

int w, h;
char field[600][600];
int springDist[600][600];
int goalDist[600][600];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int sx, sy;

inline bool Movable(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h) {
    return false;
  }
  if (field[y][x] != '.') {
    return false;
  }
  return true;
}

void CalcDist() {
  MEMSET(springDist, -1);
  MEMSET(goalDist, -1);
  queue<Point> que;
  REP(y, h) {
    REP(x, w) {
      if (field[y][x] == '*') {
        REP(dir, 4) {
          int nx = x + dx[dir];
          int ny = y + dy[dir];
          if (!Movable(nx, ny)) {
            continue;
          }
          que.push(Point(nx, ny, 1));
        }
      }
    }
  }
  while (!que.empty()) {
    Point p = que.front();
    que.pop();
    if (springDist[p.y][p.x] != -1) {
      continue;
    }
    springDist[p.y][p.x] = p.cost;
    REP(dir, 4) {
      int nx = p.x + dx[dir];
      int ny = p.y + dy[dir];
      if (!Movable(nx, ny)) {
        continue;
      }
      que.push(Point(nx, ny, p.cost + 1));
    }
  }

  REP(y, h) {
    REP(x, w) {
      if (field[y][x] == 'g') {
        que.push(Point(x, y, 0));
      }
    }
  }
  while (!que.empty()) {
    Point p = que.front();
    que.pop();
    if (goalDist[p.y][p.x] != -1) {
      continue;
    }
    goalDist[p.y][p.x] = p.cost;
    REP(dir, 4) {
      int nx = p.x + dx[dir];
      int ny = p.y + dy[dir];
      if (!Movable(nx, ny)) {
        continue;
      }
      que.push(Point(nx, ny, p.cost + 1));
    }
  }
}

double ToGoal(int x, int y, double E) {
  double ret = 1e+100;
  if (goalDist[y][x] != -1) {
    ret = min(ret, (double)goalDist[y][x]);
  }
  if (springDist[y][x] != -1) {
    ret = min(ret, springDist[y][x] + E);
  }
  // cout << x << " " << y << endl;
  assert(goalDist[y][x] != -1 || springDist[y][x] != -1);
  return ret;
}

// double vs[510 * 510];
double calc(double E) {
  // double nE = 0.0;
  int cnt = 0;
  priority_queue<double> que;
  REP(y, h) {
    REP(x, w) {
      if (field[y][x] != '.') {
        continue;
      }
      // vs[cnt++] = ToGoal(x, y, E);
      que.push(-ToGoal(x, y, E));
      // nE += ToGoal(x, y, E);
      cnt++;
    }
  }
  // sort(vs, vs + cnt);
  REP(i, cnt - 1) {
    // nE += vs[i];
    double l = que.top();
    que.pop();
    double r = que.top();
    que.pop();
    que.push(l + r);
  }
  return -que.top() / cnt;
}

int main() {
  while (scanf("%d %d", &w, &h) > 0) {
    REP(y, h) {
      scanf("%s", field[y]);
      REP(x, w) {
        if (field[y][x] == 's') {
          sx = x;
          sy = y;
          field[y][x] = '.';
        }
      }
    }
    CalcDist();
    double left = 0.0;
    double right = 1e+10;
    if (springDist[sy][sx] != -1) {
      REP(iter, 90) {
        double mid = (left + right) / 2.0;
        if (calc(mid) > mid) {
          left = mid;
        } else {
          right = mid;
        }
      }
    }
    printf("%.10f\\n", ToGoal(sx, sy, left));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Point {
  int x, y, cost;
  Point() { ; }
  Point(int x, int y, int cost) : x(x), y(y), cost(cost) { ; }
};

int w, h;
char field[600][600];
int springDist[600][600];
int goalDist[600][600];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int sx, sy;

inline bool Movable(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h) {
    return false;
  }
  if (field[y][x] != '.') {
    return false;
  }
  return true;
}

void CalcDist() {
  MEMSET(springDist, -1);
  MEMSET(goalDist, -1);
  queue<Point> que;
  REP(y, h) {
    REP(x, w) {
      if (field[y][x] == '*') {
        REP(dir, 4) {
          int nx = x + dx[dir];
          int ny = y + dy[dir];
          if (!Movable(nx, ny)) {
            continue;
          }
          que.push(Point(nx, ny, 1));
        }
      }
    }
  }
  while (!que.empty()) {
    Point p = que.front();
    que.pop();
    if (springDist[p.y][p.x] != -1) {
      continue;
    }
    springDist[p.y][p.x] = p.cost;
    REP(dir, 4) {
      int nx = p.x + dx[dir];
      int ny = p.y + dy[dir];
      if (!Movable(nx, ny)) {
        continue;
      }
      que.push(Point(nx, ny, p.cost + 1));
    }
  }

  REP(y, h) {
    REP(x, w) {
      if (field[y][x] == 'g') {
        que.push(Point(x, y, 0));
      }
    }
  }
  while (!que.empty()) {
    Point p = que.front();
    que.pop();
    if (goalDist[p.y][p.x] != -1) {
      continue;
    }
    goalDist[p.y][p.x] = p.cost;
    REP(dir, 4) {
      int nx = p.x + dx[dir];
      int ny = p.y + dy[dir];
      if (!Movable(nx, ny)) {
        continue;
      }
      que.push(Point(nx, ny, p.cost + 1));
    }
  }
}

double ToGoal(int x, int y, double E) {
  double ret = 1e+100;
  if (goalDist[y][x] != -1) {
    ret = min(ret, (double)goalDist[y][x]);
  }
  if (springDist[y][x] != -1) {
    ret = min(ret, springDist[y][x] + E);
  }
  // cout << x << " " << y << endl;
  assert(goalDist[y][x] != -1 || springDist[y][x] != -1);
  return ret;
}

// double vs[510 * 510];
double calc(double E) {
  // double nE = 0.0;
  int cnt = 0;
  priority_queue<double> que;
  REP(y, h) {
    REP(x, w) {
      if (field[y][x] != '.') {
        continue;
      }
      // vs[cnt++] = ToGoal(x, y, E);
      que.push(-ToGoal(x, y, E));
      // nE += ToGoal(x, y, E);
      cnt++;
    }
  }
  // sort(vs, vs + cnt);
  REP(i, cnt - 1) {
    // nE += vs[i];
    double l = que.top();
    que.pop();
    double r = que.top();
    que.pop();
    que.push(l + r);
  }
  return -que.top() / cnt;
}

int main() {
  while (scanf("%d %d", &w, &h) > 0) {
    REP(y, h) {
      scanf("%s", field[y]);
      REP(x, w) {
        if (field[y][x] == 's') {
          sx = x;
          sy = y;
          field[y][x] = '.';
        }
      }
    }
    CalcDist();
    double left = 0.0;
    double right = 1e+18;
    if (springDist[sy][sx] != -1) {
      REP(iter, 164) {
        double mid = (left + right) / 2.0;
        if (calc(mid) > mid) {
          left = mid;
        } else {
          right = mid;
        }
      }
    }
    printf("%.10f\\n", ToGoal(sx, sy, left));
  }
}