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

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

using namespace std;
typedef long long ll;
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 Gate {
  int x;
  double first;
  double second;
  Gate() { ; }
  Gate(int x, double f, double d) : x(x), first(f), second(d) { ; }
};

int n, m, k;
Gate gate[200];
int exist[2200];
double dp[2][2000];
double watertime[2000];
const int OFFSET = 200;

int main() {
  while (scanf("%d %d %d", &n, &m, &k), n | m | k) {
    MEMSET(exist, -1);
    MEMSET(watertime, 0);
    REP(i, n) {
      int x, ud;
      double l, f, d;
      cin >> x >> l >> f >> d >> ud;
      f = l / f;
      d = l / d;
      if (ud) {
        swap(f, d);
        watertime[x + OFFSET] = d;
      }
      gate[i] = Gate(x + OFFSET, f, d);
      exist[x + OFFSET] = i;
    }
    fill(dp[0], dp[0] + 2000, -100000);
    REP(iter, m) {
      double speed;
      cin >> speed;
      speed = 1.0 / speed;
      int next = (iter + 1) & 1;
      int prev = iter & 1;
      fill(dp[next], dp[next] + 2000, 100000000);
      int start = OFFSET - iter;
      dp[next][start] = 0;
      FOR(x, start, k + OFFSET * 2) {
        if (exist[x] == -1) {
          dp[next][x + 1] = dp[next][x] + speed;
          ;
        } else if (exist[x] != -1) {
          int index = exist[x];
          double w = watertime[x];
          dp[next][x + 1] = max(dp[next][x], w) + gate[index].first + speed;
          watertime[x] =
              max(dp[next][x], w) + gate[index].first + gate[index].second;
        }
        dp[next][x + 1] = max(dp[next][x + 1], dp[prev][x + 2]);
      }
    }
    printf("%.6lf\\n", dp[m & 1][k + OFFSET]);
  }
}

Error: WA

Fixed submission:

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

using namespace std;
typedef long long ll;
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 Gate {
  int x;
  double first;
  double second;
  Gate() { ; }
  Gate(int x, double f, double d) : x(x), first(f), second(d) { ; }
};

int n, m, k;
Gate gate[200];
int exist[1200];
double dp[2][2000];
double watertime[2000];
const int OFFSET = 200;

int main() {
  while (scanf("%d %d %d", &n, &m, &k), n | m | k) {
    MEMSET(exist, -1);
    MEMSET(watertime, 0);
    REP(i, n) {
      int x, ud;
      double l, f, d;
      cin >> x >> l >> f >> d >> ud;
      f = l / f;
      d = l / d;
      if (ud) {
        swap(f, d);
        watertime[x + OFFSET] = d;
      }
      gate[i] = Gate(x + OFFSET, f, d);
      exist[x + OFFSET] = i;
    }
    fill(dp[0], dp[0] + 2000, -100000);
    REP(iter, m) {
      double speed;
      cin >> speed;
      speed = 1.0 / speed;
      int next = (iter + 1) & 1;
      int prev = iter & 1;
      fill(dp[next], dp[next] + 2000, 100000000);
      int start = OFFSET - iter;
      dp[next][start] = 0;
      FOR(x, start, k + OFFSET * 2) {
        if (exist[x] == -1) {
          dp[next][x + 1] = dp[next][x] + speed;
          ;
        } else if (exist[x] != -1) {
          int index = exist[x];
          double w = watertime[x];
          dp[next][x + 1] = max(dp[next][x], w) + gate[index].first + speed;
          watertime[x] =
              max(dp[next][x], w) + gate[index].first + gate[index].second;
        }
        dp[next][x + 1] = max(dp[next][x + 1], dp[prev][x + 2]);
      }
    }
    printf("%.6lf\\n", dp[m & 1][k + OFFSET]);
  }
}