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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef map<long long, int> U;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

U factor(long long N) {
  U p;
  for (int i = 2; i * i <= N; i++) {
    while (N % i == 0)
      p[i]++, N /= i;
  }
  if (N != 1)
    p[N]++;
  return p;
}
U gcd(U a, U b) {
  U c;
  for (U::iterator it = a.begin(); it != a.end(); ++it) {
    if (b.count(it->first))
      continue;
    c[it->first] = min(it->second, b[it->first]);
  }
  return c;
}
U lcm(U a, U b) {
  U c;
  for (U::iterator it = a.begin(); it != a.end(); ++it) {
    c[it->first] = max(c[it->first], it->second);
  }
  for (U::iterator it = b.begin(); it != b.end(); ++it) {
    c[it->first] = max(c[it->first], it->second);
  }
  return c;
}
U mul(U a, U b) {
  for (U::iterator it = a.begin(); it != a.end(); ++it) {
    b[it->first] += it->second;
  }
  return b;
}
U div(U a, U b) {
  for (U::iterator it = b.begin(); it != b.end(); ++it) {
    a[it->first] -= it->second;
  }
  return a;
}
long long conv(U a) {
  long long res = 1;
  for (U::iterator it = a.begin(); it != a.end(); ++it) {
    for (int j = 0; j < it->second; j++)
      res *= it->first;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;

  while (cin >> n && n) {
    U A[10], B[10] = {};
    U l;
    for (int i = 0; i < n; i++) {
      long long a_, b_;
      cin >> a_ >> b_;
      U a = factor(a_);
      U b = factor(b_);
      U g = gcd(a, b);
      a = div(a, g);
      b = div(b, g);
      l = lcm(l, b);
      A[i] = a;
      B[i] = b;
    }
    U L;
    for (int i = 0; i < n; i++) {
      U a = div(mul(A[i], l), B[i]);
      // cout << a << endl;
      B[i] = l;
      A[i] = a;
      L = lcm(a, L);
    }
    for (int i = 0; i < n; i++) {
      cout << conv(div(L, A[i])) << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct NODE {
  int to, cost, ticket;
  NODE(int to, int cost, int ticket) : to(to), cost(cost), ticket(ticket) {}
  NODE() {}
};

bool operator<(const NODE &a, const NODE &b) { return a.cost > b.cost; }

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d && n) {
    s--, d--;
    vector<NODE> e[100];
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      a--, b--;
      e[a].push_back(NODE(b, f, -1));
      e[b].push_back(NODE(a, f, -1));
    }
    int done[110][15] = {};
    priority_queue<NODE> Q;
    Q.push(NODE(s, 0, c));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (done[q.to][q.ticket]++)
        continue;
      else
        done[q.to][q.ticket] = true;
      if (q.to == d) {
        cout << q.cost << endl;
        break;
      }
      for (int i = 0; i < e[q.to].size(); i++) {
        Q.push(NODE(e[q.to][i].to, q.cost + e[q.to][i].cost, q.ticket));
        if (q.ticket)
          Q.push(
              NODE(e[q.to][i].to, q.cost + e[q.to][i].cost / 2, q.ticket - 1));
      }
    }
  }
}