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

#include <climits>
#include <cstdio>
#include <iostream>
#include <vector>

struct Edge {
  Edge(int t, int d) : to(t), distance(d){};
  int to, distance;
};
void f(int s, int t, int d, std::vector<std::vector<int>> &distance,
       std::vector<std::vector<int>> &to, std::vector<std::vector<int>> &from) {
  distance.at(s).at(t) = d;
  to.at(s).push_back(t);
  from.at(t).push_back(s);
}
int main() {
  int v, e, r;
  std::scanf("%d %d %d", &v, &e, &r);
  // v = 4; e = 6; r = 0;
  std::vector<std::vector<int>> distance(v, std::vector<int>(v, 1000000000));
  std::vector<std::vector<int>> to(v);
  std::vector<std::vector<int>> from(v);
  for (auto i = 0; i < e; ++i) {
    int s, t, d;
    std::scanf("%d %d %d", &s, &t, &d);
    distance.at(s).at(t) = d;
    to.at(s).push_back(t);
    from.at(t).push_back(s);
  }
  // f(0,1,2,distance,to,from);f(0,2,3,distance,to,from);f(1,2,-5,distance,to,from);f(1,3,1,distance,to,from);f(2,3,2,distance,to,from);f(3,1,0,distance,to,from);
  bool negative = false;
  for (auto i = 0; i < v; ++i) {
    distance.at(i).at(i) = 0;
  }
  for (auto k = 0; k < v; ++k) {
    for (auto i : from.at(k)) {
      for (auto j : to.at(k)) {
        // std::cout << "k = " << k << " i = " << i << " j = " << j << "dist" <<
        // distance.at(i).at(j) << "-" << distance.at(i).at(k) << "-" <<
        // distance.at(k).at(j) << std::endl;
        if (distance.at(i).at(j) >
            distance.at(i).at(k) + distance.at(k).at(j)) {
          if (distance.at(i).at(j) > 10000000 and i != j) {
            to.at(i).push_back(j);
            from.at(j).push_back(i);
          }
          distance.at(i).at(j) = distance.at(i).at(k) + distance.at(k).at(j);
        }
      }
      if (distance.at(i).at(i) < 0 and distance.at(r).at(i) <= 10000000) {
        negative = true;
        k = v - 1;
      }
    }
  }
  for (auto i = 0; i < to.at(r).size(); ++i) {
    if (negative) {
      i = to.at(r).size();
    } else {
      negative = (distance.at(to.at(r).at(i)).at(to.at(r).at(i)) < 0);
    }
  }
  if (negative) {
    std::printf("NEGATIVE CYCLE\\n");
  } else {
    for (auto i : distance.at(r)) {
      if (i <= 10000000) {
        std::printf("%d\\n", i);
      } else {
        std::printf("INF\\n");
      }
    }
  }
}

Error: 0

Fixed submission:

#include <climits>
#include <cstdio>
#include <vector>

struct Edge {
  Edge(int t, int d) : to(t), distance(d){};
  int to, distance;
};
void f(int s, int t, int d, std::vector<std::vector<int>> &distance,
       std::vector<std::vector<int>> &to, std::vector<std::vector<int>> &from) {
  distance.at(s).at(t) = d;
  to.at(s).push_back(t);
  from.at(t).push_back(s);
}
int main() {
  int v, e, r;
  std::scanf("%d %d %d", &v, &e, &r);
  // v = 4; e = 6; r = 0;
  std::vector<std::vector<int>> distance(v, std::vector<int>(v, 1000000000));
  std::vector<std::vector<int>> to(v);
  std::vector<std::vector<int>> from(v);
  for (auto i = 0; i < e; ++i) {
    int s, t, d;
    std::scanf("%d %d %d", &s, &t, &d);
    distance.at(s).at(t) = d;
    to.at(s).push_back(t);
    from.at(t).push_back(s);
  }
  // f(0,1,2,distance,to,from);f(0,2,3,distance,to,from);f(1,2,-5,distance,to,from);f(1,3,1,distance,to,from);f(2,3,2,distance,to,from);f(3,1,0,distance,to,from);
  bool negative = false;
  for (auto i = 0; i < v; ++i) {
    distance.at(i).at(i) = 0;
  }
  for (auto k = 0; k < v; ++k) {
    for (auto i : from.at(k)) {
      for (auto j : to.at(k)) {
        // std::cout << "k = " << k << " i = " << i << " j = " << j << "dist" <<
        // distance.at(i).at(j) << "-" << distance.at(i).at(k) << "-" <<
        // distance.at(k).at(j) << std::endl;
        if (distance.at(i).at(j) >
            distance.at(i).at(k) + distance.at(k).at(j)) {
          if (distance.at(i).at(j) > 10000000 and i != j) {
            to.at(i).push_back(j);
            from.at(j).push_back(i);
          }
          distance.at(i).at(j) = distance.at(i).at(k) + distance.at(k).at(j);
        }
      }
      if (distance.at(i).at(i) < 0 and distance.at(r).at(i) <= 10000000) {
        negative = true;
        k = v;
      }
    }
  }
  for (auto i = 0; i < to.at(r).size(); ++i) {
    if (negative) {
      i = to.at(r).size();
    } else {
      negative = distance.at(to.at(r).at(i)).at(to.at(r).at(i)) < 0;
    }
  }
  if (negative) {
    std::printf("NEGATIVE CYCLE\\n");
  } else {
    for (auto i : distance.at(r)) {
      if (i <= 10000000) {
        std::printf("%d\\n", i);
      } else {
        std::printf("INF\\n");
      }
    }
  }
}