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

#include <complex>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef complex<double> P;
#define EPS 1e-5
#define INF 1e7

typedef pair<int, double> Node;

class State {
public:
  int n;
  double c;
  State(int n, double c) : n(n), c(c) {}

  bool operator<(const State &s) const { return c > s.c; }
};

// üª
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

// OÏ
double cross(const P &a, const P &b) { return imag(conj(a) * b); }

double dist(const P &a, const P &b) { return abs(a - b); }

bool intersect(L &a, L &b) {
  double t = max(a[0].imag(), a[1].imag());
  double l = min(a[0].imag(), a[1].imag());

  return (t + EPS > b[0].imag() && b[0].imag() > l - EPS);
}

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0]; // same line
  return m[0] + B / A * (m[1] - m[0]);
}

int main() {
  cout << setprecision(10) << setiosflags(ios::fixed);
  double vw, vc;
  int N;

  while (cin >> N, N) {
    vector<P> in;

    cin >> vw >> vc;
    for (int i = 0; i < N; i++) {
      double x, y;
      cin >> x >> y;
      in.push_back(P(x, y));
    }

    vector<Node> node[4000];
    for (int i = 0; i < N - 1; i++) {
      node[i].push_back(make_pair(i + 1, dist(in[i], in[i + 1]) / vw));
    }

    for (int i = 0; i < N; i++) {
      L hz(P(-INF, in[i].imag()), P(INF, in[i].imag()));
      for (int j = i - 2; j >= 0; j--) {
        if (in[i].imag() + EPS > in[j + 1].imag())
          break;
        L tmp(in[j], in[j + 1]);
        if (intersect(tmp, hz)) {
          P xp = crosspoint(hz, tmp);
          in.push_back(xp);
          int p = in.size();
          node[j].push_back(make_pair(p, dist(in[j], xp) / vw));
          node[p].push_back(make_pair(j + 1, dist(in[j + 1], xp) / vw));
          node[p].push_back(make_pair(i, dist(in[i], xp) / vc));

          break;
        }
      }
      for (int j = i + 1; j < N - 1; j++) {
        if (in[i].imag() + EPS > in[j].imag())
          break;

        L tmp(in[j], in[j + 1]);
        if (intersect(tmp, hz)) {
          P xp = crosspoint(hz, tmp);
          in.push_back(xp);
          int p = in.size();
          node[j].push_back(make_pair(p, dist(in[j], xp) / vw));
          node[p].push_back(make_pair(j + 1, dist(in[j + 1], xp) / vw));
          node[i].push_back(make_pair(p, dist(in[i], xp) / vc));

          break;
        }
      }
    }

    priority_queue<State> q;
    q.push(State(0, 0));
    bool v[4000] = {0};

    while (!q.empty()) {
      State s = q.top();
      q.pop();
      if (v[s.n])
        continue;
      v[s.n] = 1;

      if (s.n == N - 1) {
        cout << s.c << endl;
        break;
      }

      for (int i = 0; i < node[s.n].size(); i++) {
        Node next = node[s.n][i];
        if (v[next.first])
          continue;

        q.push(State(next.first, s.c + next.second));
      }
    }
  }
}

Error: WA

Fixed submission:

#include <complex>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef complex<double> P;
#define EPS 1e-5
#define INF 1e7

typedef pair<int, double> Node;

class State {
public:
  int n;
  double c;
  State(int n, double c) : n(n), c(c) {}

  bool operator<(const State &s) const { return c > s.c; }
};

// üª
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

// OÏ
double cross(const P &a, const P &b) { return imag(conj(a) * b); }

double dist(const P &a, const P &b) { return abs(a - b); }

bool near(double a, double b) { return (a - b) < EPS; }

bool intersect(L &a, L &b) {
  double t = max(a[0].imag(), a[1].imag());
  double l = min(a[0].imag(), a[1].imag());

  return (t + EPS > b[0].imag() && b[0].imag() > l - EPS);
}

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0]; // same line
  return m[0] + B / A * (m[1] - m[0]);
}

int main() {
  cout << setprecision(10) << setiosflags(ios::fixed);
  double vw, vc;
  int N;

  while (cin >> N, N) {
    vector<P> in;

    cin >> vw >> vc;
    for (int i = 0; i < N; i++) {
      double x, y;
      cin >> x >> y;
      in.push_back(P(x, y));
    }

    vector<Node> node[300];
    for (int i = 0; i < N - 1; i++) {
      node[i].push_back(make_pair(i + 1, dist(in[i], in[i + 1]) / vw));
    }

    for (int i = 0; i < N; i++) {
      L hz(P(-INF, in[0].imag()), P(INF, in[0].imag()));
      for (int j = i - 2; j >= 0; j--) {
        L tmp(in[j], in[j + 1]);
        if (intersect(hz, tmp)) {
          P xp = crosspoint(hz, tmp);
          if (tmp[0].imag() - EPS < xp.imag() &&
              tmp[1].imag() + EPS > xp.imag()) {
            in.push_back(xp);
            int p = in.size();
            node[j].push_back(make_pair(p, dist(in[j], xp) / vw));
            node[p].push_back(make_pair(j + 1, dist(in[j + 1], xp) / vw));
            node[p].push_back(make_pair(i, dist(in[i], xp) / vc));
          }
        }

        break;
      }
      for (int j = i + 1; j < N - 1; j++) {
        L tmp(in[j], in[j + 1]);
        if (intersect(hz, tmp)) {
          P xp = crosspoint(hz, tmp);
          if (in[j].imag() - EPS < in[j + 1].imag()) {
            in.push_back(xp);
            int p = in.size();
            node[j].push_back(make_pair(p, dist(in[j], xp) / vw));
            node[p].push_back(make_pair(j + 1, dist(in[j + 1], xp) / vw));
            node[i].push_back(make_pair(p, dist(in[i], xp) / vc));
          }

          break;
        }
      }
    }

    priority_queue<State> q;
    q.push(State(0, 0));
    bool v[3000] = {0};

    while (!q.empty()) {
      State s = q.top();
      q.pop();
      if (v[s.n])
        continue;
      v[s.n] = 1;

      if (s.n == N - 1) {
        cout << s.c << endl;
        break;
      }

      for (int i = 0; i < node[s.n].size(); i++) {
        Node next = node[s.n][i];
        if (v[next.first])
          continue;

        q.push(State(next.first, s.c + next.second));
      }
    }
  }
}