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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))

double dot(P a, P b) { return (a.X * b.X + a.Y * b.Y); }
double cross(P a, P b) { return (a.X * b.Y - a.Y * b.X); }

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

// 2??´?????????????????? (?????????0??§???????????¨)
bool is_parallel(P a1, P a2, P b1, P b2) {
  return EQ(cross(a1 - a2, b1 - b2), 0.0);
}

P crossp_ll(P a1, P a2, P b1, P b2) {
  double d1 = cross(b2 - b1, b1 - a1);
  double d2 = cross(b2 - b1, a2 - a1);
  if (EQ(d1, 0) && EQ(d2, 0))
    return a1; // same line
  if (EQ(d2, 0))
    assert(false); // precondition not satisfied
  return a1 + d1 / d2 * (a2 - a1);
}

int N;

signed main() {
  while (cin >> N, N) {
    vector<L> ls;
    rep(i, 0, N) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      ls.push_back(make_pair(P(a, b), P(c, d)));
    }

    int ans = 1;
    rep(i, 0, N) {
      set<pii> s;
      rep(j, 0, i) {
        if (is_parallel(ls[i].first, ls[i].second, ls[j].first, ls[j].second))
          continue;
        P cp = crossp_ll(ls[i].first, ls[i].second, ls[j].first, ls[j].second);
        if (-100.0 + EPS < cp.X && cp.X + EPS < 100.0 && -100.0 + EPS < cp.Y &&
            cp.Y + EPS < 100.0) {
          // printf("point: %lf %lf\\n", cp.X, cp.Y);
          int x = floorl(cp.X * pow(10, 10));
          int y = floorl(cp.Y * pow(10, 10));
          s.insert(pii(x, y));
        }
      }
      // printf("size = %lld\\n", s.size());
      ans += (s.size() + 1);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, A, S, Goal;

struct Elem {
  int point;
  string s;
};

struct Edge {
  int to;
  string s;
};

signed main() {
  while (cin >> N >> A >> S >> Goal, N) {
    // dp ??????, cnt ???????????????
    vector<string> dp(N, "");
    vector<int> cnt(N, 0);

    vector<vector<Edge>> G(N);
    rep(i, 0, A) {
      int a, b;
      string s;
      cin >> a >> b >> s;
      G[b].push_back(Edge{a, s});
    }

    bool ng = false;
    rep(k, 0, 7 * N) {
      rep(i, 0, N) {
        if (i != Goal && dp[i] == "")
          continue;
        for (auto e : G[i]) {
          int to = e.to;
          string ss = e.s;
          if (dp[to] == "" || dp[to] > ss + dp[i]) {
            dp[to] = ss + dp[i];
            if (k == 7 * N - 1)
              ng = true;
          }
        }
      }
    }

    if (ng || dp[S] == "")
      cout << "NO" << endl;
    else
      cout << dp[S] << endl;
  }
  return 0;
}