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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

ld dis(pair<ld, ld> l, pair<ld, ld> r) {
  ld dx = l.first - r.first;
  ld dy = l.second - r.second;
  return sqrt(ld(dx) * dx + ld(dy) * dy);
}

ld get_x(pair<ld, ld> f, pair<ld, ld> t, ld y) {
  if (f.second > t.second)
    return get_x(t, f, y);

  ld a = y - f.second;
  ld b = t.second - y;

  return (t.first * a + f.first * b) / (a + b);
}

int main() {
  cout << setprecision(10) << fixed;
  while (true) {
    int N;
    ld vw, vc;
    cin >> N;
    if (!N)
      break;
    cin >> vw >> vc;
    vector<pair<ld, ld>> points(N);

    for (int i = 0; i < N; ++i) {
      int a, b;
      cin >> a >> b;
      points[i] = make_pair(a, b);
    }

    vector<vector<pair<int, ld>>> edges(N);
    for (int i = 0; i < N - 1; ++i) {
      edges[i].push_back(make_pair(i + 1, dis(points[i], points[i + 1]) / vw));
    }

    for (int i = 0; i < N - 1; ++i) {
      ld ny = points[i].second;
      if (points[i + 1].second > ny) {
        for (int j = i + 1; j < N - 1; ++j) {
          ld fy = points[j].second;
          ld ty = points[j + 1].second;

          if (ny < fy && ty <= ny) {
            ld tunnel_x = get_x(points[j], points[j + 1], ny);

            ld adis = abs(tunnel_x - points[i].first) / vc +
                      dis(make_pair(tunnel_x, ny), points[j + 1]) / vw;

            edges[i].push_back(make_pair(j + 1, adis));
            break;
          }
        }
      }
    }
    for (int i = 1; i < N; ++i) {
      ld ny = points[i].second;
      if (points[i - 1].second > ny) {
        for (int j = i - 1; j > 0; --j) {
          ld fy = points[j].second;
          ld ty = points[j - 1].second;

          if (ny < fy && ty <= ny) {
            ld tunnel_x = get_x(points[j], points[j - 1], ny);

            ld adis = abs(tunnel_x - points[i].first) / vc +
                      dis(make_pair(tunnel_x, ny), points[j - 1]) / vw;

            edges[j - 1].push_back(make_pair(i, adis));
            break;
          }
        }
      }
    }

    vector<ld> memo(N, 1e9);
    memo[0] = 0;
    for (int i = 0; i < N; ++i) {
      for (auto e : edges[i]) {
        int to = e.first;
        ld cost = e.second;
        memo[to] = min(memo[to], memo[i] + cost);
      }
    }
    cout << memo[N - 1] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

int adiv(int a) {
  map<int, int> mp;
  int num(a);
  for (int i = 2; i <= a; ++i) {
    while (num % i == 0) {
      mp[i]++;
      num /= i;
    }
  }
  int anum = 0;
  for (auto m : mp) {
    anum -= m.first;
  }
  anum += 2 * prev(mp.end())->first;
  return anum;
}

double dis(pair<int, int> l, pair<int, int> r) {
  int dx = l.first - r.first;
  int dy = l.second - r.second;
  return sqrt(double(dx) * dx + double(dy) * dy);
}

double get_x(pair<int, int> f, pair<int, int> t, int y) {
  if (f.second > t.second)
    return get_x(t, f, y);

  double a = y - f.second;
  double b = t.second - y;

  return (f.first * b + f.second * a) / (a + b);
}

int main() {
  cout << setprecision(10) << fixed;
  while (true) {
    int N;
    double vw, vc;
    cin >> N;
    if (!N)
      break;
    cin >> vw >> vc;
    vector<pair<int, int>> points(N);

    for (int i = 0; i < N; ++i) {
      int a, b;
      cin >> a >> b;
      points[i] = make_pair(a, b);
    }

    vector<vector<pair<int, double>>> edges(N);
    for (int i = 0; i < N - 1; ++i) {
      edges[i].push_back(make_pair(i + 1, dis(points[i], points[i + 1]) / vw));
    }

    int nid = N;

    for (int i = 0; i < N - 1; ++i) {
      int ny = points[i].second;
      if (points[i + 1].second > ny) {
        for (int j = i + 1; j < N - 1; ++j) {
          int fy = points[j].second;
          int ty = points[j + 1].second;

          if (ny < fy && ty <= ny) {
            double tunnel_x = get_x(points[j], points[j + 1], ny);

            double adis = abs(tunnel_x - points[i].first) / vc +
                          dis(make_pair(tunnel_x, ny), points[j + 1]) / vw;

            edges[i].push_back(make_pair(j + 1, adis));
            break;
          }
        }
      }
    }
    for (int i = 1; i < N; ++i) {
      int ny = points[i].second;
      if (points[i - 1].second > ny) {
        for (int j = i - 1; j > 0; ++j) {
          int fy = points[j].second;
          int ty = points[j - 1].second;

          if (ny < fy && ty <= ny) {
            double tunnel_x = get_x(points[j], points[j - 1], ny);

            double adis = abs(tunnel_x - points[i].first) / vc +
                          dis(make_pair(tunnel_x, ny), points[j - 1]) / vw;

            edges[j - 1].push_back(make_pair(i, adis));
            break;
          }
        }
      }
    }

    vector<double> memo(N, 1e9);
    memo[0] = 0;
    for (int i = 0; i < N; ++i) {
      for (auto e : edges[i]) {
        int to = e.first;
        double cost = e.second;
        memo[to] = min(memo[to], memo[i] + cost);
      }
    }
    cout << memo[N - 1] << endl;
  }
  return 0;
}