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

#include <cmath>
#include <complex>
#include <cstdio>
#include <vector>
using namespace std;

#define EPS 1e-7

typedef complex<double> P;

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

double angle(P a, P b, P c) { return arg((c - b) / (b - a)); }

double dot(const P &a, const P &b) { return real(conj(a) * b); }

double cross(const P &a, const P &b) { return imag(conj(a) * b); }

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;
}

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

int main() {
  int N;
  while (scanf("%d", &N), N) {
    vector<P> v(N);
    vector<bool> used(N, false);
    for (int i = 0; i < N; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      v[i] = P(x, y);
    }

    double ans = 0;
    P a = P(0, -1), b = P(0, 0);
    for (int i = 0; i < N; i++) {
      int select = -1;
      double alg = 0;
      for (int j = 0; j < N; j++) {
        if (used[j])
          continue;
        if (ccw(a, b, v[j]) > 0)
          continue;

        double t = angle(a, b, v[j]);
        if (select == -1) {
          alg = t;
          select = j;
        } else if (isequal(alg, t)) {
          if (dist(b, v[j]) < dist(b, v[select]))
            select = j;
        } else if (t > alg) {
          alg = t;
          select = j;
        }
      }

      used[select] = true;
      a = b;
      b = v[select];
      ans += dist(a, b);
    }

    printf("%.1lf\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <cmath>
#include <complex>
#include <cstdio>
#include <vector>
using namespace std;

typedef complex<double> P;

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

double angle(P a, P b, P c) { return arg((c - b) / (b - a)); }

double dot(const P &a, const P &b) { return real(conj(a) * b); }

double cross(const P &a, const P &b) { return imag(conj(a) * b); }

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;
}

int main() {
  int N;
  while (scanf("%d", &N), N) {
    vector<P> v(N);
    vector<bool> used(N, false);
    for (int i = 0; i < N; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      v[i] = P(x, y);
    }

    double ans = 0;
    P a = P(0, -1), b = P(0, 0);
    for (int i = 0; i < N; i++) {
      int select = -1;
      double alg = 0;
      for (int j = 0; j < N; j++) {
        if (used[j])
          continue;
        if (ccw(a, b, v[j]) > 0)
          continue;

        double t = angle(a, b, v[j]);
        if (select == -1) {
          alg = t;
          select = j;
        } else if (t == alg) {
          if (dist(b, v[j]) < dist(b, v[select]))
            select = j;
        } else if (t > alg) {
          alg = t;
          select = j;
        }
      }

      used[select] = true;
      a = b;
      b = v[select];
      ans += dist(a, b);
    }

    printf("%.1lf\\n", ans);
  }
}