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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;

constexpr ll INF = 1LL << 60;

int N;
void WarshallFloyd(vector<vector<ll>> &d) {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (d[i][j] > d[i][k] + d[k][j] and d[i][k] < INF and d[k][j] < INF) {
          d[i][j] = d[i][k] + d[k][j];
        }
      }
    }
  }
}

template <typename T> struct Matrix {
  Matrix(const int n) : Matrix{n, n} {}
  Matrix(const int r, const int c)
      : R{r}, C{c}, table(r, vector<T>(c, static_cast<T>(0))) {}
  vector<T> &operator[](const int n) { return table[n]; }
  const vector<T> &operator[](const int n) const { return table[n]; }

  Matrix &operator+=(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] += mat[i][j];
      }
    }
    return *this;
  }

  Matrix &operator-=(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] -= mat[i][j];
      }
    }
    return *this;
  }

  Matrix &operator*=(const T val) {
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] *= val;
      }
    }
    return *this;
  }

  Matrix &operator/=(const T val) {
    assert(val != 0);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] /= val;
      }
    }
    return *this;
  }

  Matrix operator-() const {
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result[i][j] = -table[i][j];
      }
    }
    return result;
  }
  Matrix operator+(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result[i][j] = table[i][j] + mat[i][j];
      }
    }
    return result;
  }

  Matrix operator-(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result.table[i][j] = table[i][j] - mat.table[i][j];
      }
    }
    return result;
  }

  Matrix operator*(const T val) {
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result[i][j] = table[i][j] * val;
      }
    }
    return result;
  }

  Matrix operator*(const Matrix &mat) {
    assert(C == mat.R);
    Matrix result(R, mat.C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < mat.C; j++) {
        T sum = 0;
        for (int k = 0; k < C; k++) {
          sum += table[i][k] * mat[k][j];
        }
        result[i][j] = sum;
      }
    }
    return result;
  }

  Matrix transposed() const {
    Matrix result(C, R);
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < R; j++) {
        result[i][j] = table[j][i];
      }
    }
    return result;
  }
  Matrix &transpose() {
    if (R == C) {
      for (int i = 0; i < R; i++) {
        for (int j = i + 1; j < R; j++) {
          swap(table[i][j], table[j][i]);
        }
      }
      return *this;
    } else {
      return *this = transposed();
    }
  }

  int R;
  int C;
  vector<vector<T>> table;
};

template <typename T>
inline Matrix<T> operator*(const T val, const Matrix<T> &mat) {
  Matrix<T> result(mat.R, mat.C);
  for (int i = 0; i < mat.R; i++) {
    for (int j = 0; j < mat.C; j++) {
      result[i][j] = val * mat[i][j];
    }
  }
  return result;
}

template <typename T>
inline ostream &operator<<(ostream &os, const Matrix<T> &mat) {
  os << "Mat<" << mat.R << "," << mat.C << ">" << endl;
  for (int i = 0; i < mat.R; i++) {
    os << "[";
    for (int j = 0; j < mat.C; j++) {
      os << mat[i][j] << ",";
    }
    os << "]" << endl;
  }
  return os;
}
template <typename T> struct Vector {
  Vector(const int n) : R(n), table(n, 0){};
  T &operator[](const int n) { return table[n]; }
  const T &operator[](const int n) const { return table[n]; }
  Vector &operator+=(const Vector &v) {
    assert(R == v.R);
    for (int i = 0; i < R; i++) {
      table[i] += v[i];
    }
    return *this;
  }
  Vector &operator-=(const Vector &v) {
    assert(R == v.R);
    for (int i = 0; i < R; i++) {
      table[i] -= v[i];
    }
    return *this;
  }
  Vector &operator*=(const T &s) {
    for (int i = 0; i < R; i++) {
      table[i] *= s;
    }
    return *this;
  }
  Vector &operator/=(const T &s) {
    assert(s != 0.0);
    for (int i = 0; i < R; i++) {
      table[i] /= s;
    }
    return *this;
  }
  Vector operator+(const Vector &v) const {
    assert(R == v.R);
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] + v[i];
    }
    return result;
  }
  Vector operator-(const Vector &v) const {
    assert(R == v.R);
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] - v[i];
    }
    return result;
  }
  Vector operator*(const T &s) const {
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] * s;
    }
    return result;
  }
  Vector operator/(const T &s) const {
    assert(s != 0.0);
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] / s;
    }
    return result;
  }
  Vector operator-() const {
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = -table[i];
    }
    return result;
  }

  int R;
  vector<T> table;
};

template <typename T>
inline Vector<T> operator*(const T &s, const Vector<T> &v) {
  Vector<T> result(v.R);
  for (int i = 0; i < v.R; i++) {
    result[i] = s * v[i];
  }
  return result;
}

template <typename T>
inline Vector<T> operator*(const Matrix<T> &mat, const Vector<T> &v) {
  assert(mat.C == v.R);
  Vector<T> result(mat.R);
  for (int i = 0; i < v.R; i++) {
    for (int j = 0; j < mat.R; j++) {
      result[i] += mat[i][j] * v[j];
    }
  }
  return result;
}

template <typename T>
inline ostream &operator<<(ostream &os, const Vector<T> &v) {
  os << "Vec<" << v.R << ">" << endl;
  os << "[";
  for (int i = 0; i < v.R; i++) {
    os << v[i] << ",";
  }
  os << "]^T" << endl;
  return os;
}
template <typename T>
Vector<T> GaussJordan(const Matrix<T> &mat, const Vector<T> &v) {
  assert(mat.R == mat.C);
  const int N = mat.R;
  Matrix<T> A(N, N + 1);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      A[i][j] = mat[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    A[i][N] = v[i];
  }

  for (int i = 0; i < N; i++) {
    int pivot = i;
    for (int j = i; j < N; j++) {
      if (abs(A[j][i]) > abs(A[pivot][i])) {
        pivot = j;
      }
    }
    assert(A[pivot][i]);
    swap(A[i], A[pivot]);
    for (int j = i + 1; j <= N; j++) {
      A[i][j] /= A[i][i];
    }
    for (int j = 0; j < N; j++) {
      if (i != j) {
        for (int k = i + 1; k <= N; k++) {
          A[j][k] -= A[j][i] * A[i][k];
        }
      }
    }
  }
  Vector<T> res(N);
  for (int i = 0; i < N; i++) {
    res[i] = A[i][N];
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N;
    int S, T;
    cin >> S >> T;
    if (N == 0 and S == 0 and T == 0) {
      break;
    }
    S--, T--;
    vector<bool> q(N);
    for (int i = 0; i < N; i++) {
      int b;
      cin >> b;
      q[i] = b == 1;
    }
    vector<vector<ll>> g(N, vector<ll>(N, INF));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        ll D;
        cin >> D;
        g[i][j] = D == 0 ? INF : D;
      }
    }
    auto d = g;
    for (int i = 0; i < N; i++) {
      d[i][i] = 0;
    }
    WarshallFloyd(d);
    if (d[S][T] == INF) {
      cout << "impossible" << endl;
      continue;
    }

    Matrix<ld> mat(N, N);
    Vector<ld> vec(N);
    for (int i = 0; i < N; i++) {
      if (i == T) {
        mat[i][T] = 1;
        vec[i] = 0;
        continue;
      }
      int dim = 0;
      ll sum = 0;
      if (q[i]) {
        for (int j = 0; j < N; j++) {
          if (i == j) {
            continue;
          }

          if (g[i][j] != INF and d[i][T] == d[j][T] + g[i][j]) {
            dim++;
            sum += g[i][j];
            mat[i][j] = -1;
          }
        }
      } else {
        for (int j = 0; j < N; j++) {
          if (g[i][j] != INF) {
            dim++;
            sum += g[i][j];
            mat[i][j] = -1;
          }
        }
      }
      mat[i][i] = dim;
      vec[i] = sum;
    }
    cout << fixed << setprecision(15) << GaussJordan(mat, vec)[S] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;

constexpr ll INF = 1LL << 60;

int N;
void WarshallFloyd(vector<vector<ll>> &d) {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (d[i][j] > d[i][k] + d[k][j] and d[i][k] < INF and d[k][j] < INF) {
          d[i][j] = d[i][k] + d[k][j];
        }
      }
    }
  }
}

template <typename T> struct Matrix {
  Matrix(const int n) : Matrix{n, n} {}
  Matrix(const int r, const int c)
      : R{r}, C{c}, table(r, vector<T>(c, static_cast<T>(0))) {}
  vector<T> &operator[](const int n) { return table[n]; }
  const vector<T> &operator[](const int n) const { return table[n]; }

  Matrix &operator+=(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] += mat[i][j];
      }
    }
    return *this;
  }

  Matrix &operator-=(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] -= mat[i][j];
      }
    }
    return *this;
  }

  Matrix &operator*=(const T val) {
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] *= val;
      }
    }
    return *this;
  }

  Matrix &operator/=(const T val) {
    assert(val != 0);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        table[i][j] /= val;
      }
    }
    return *this;
  }

  Matrix operator-() const {
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result[i][j] = -table[i][j];
      }
    }
    return result;
  }
  Matrix operator+(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result[i][j] = table[i][j] + mat[i][j];
      }
    }
    return result;
  }

  Matrix operator-(const Matrix &mat) {
    assert(R == mat.R and C == mat.C);
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result.table[i][j] = table[i][j] - mat.table[i][j];
      }
    }
    return result;
  }

  Matrix operator*(const T val) {
    Matrix result(R, C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        result[i][j] = table[i][j] * val;
      }
    }
    return result;
  }

  Matrix operator*(const Matrix &mat) {
    assert(C == mat.R);
    Matrix result(R, mat.C);
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < mat.C; j++) {
        T sum = 0;
        for (int k = 0; k < C; k++) {
          sum += table[i][k] * mat[k][j];
        }
        result[i][j] = sum;
      }
    }
    return result;
  }

  Matrix transposed() const {
    Matrix result(C, R);
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < R; j++) {
        result[i][j] = table[j][i];
      }
    }
    return result;
  }
  Matrix &transpose() {
    if (R == C) {
      for (int i = 0; i < R; i++) {
        for (int j = i + 1; j < R; j++) {
          swap(table[i][j], table[j][i]);
        }
      }
      return *this;
    } else {
      return *this = transposed();
    }
  }

  int R;
  int C;
  vector<vector<T>> table;
};

template <typename T>
inline Matrix<T> operator*(const T val, const Matrix<T> &mat) {
  Matrix<T> result(mat.R, mat.C);
  for (int i = 0; i < mat.R; i++) {
    for (int j = 0; j < mat.C; j++) {
      result[i][j] = val * mat[i][j];
    }
  }
  return result;
}

template <typename T>
inline ostream &operator<<(ostream &os, const Matrix<T> &mat) {
  os << "Mat<" << mat.R << "," << mat.C << ">" << endl;
  for (int i = 0; i < mat.R; i++) {
    os << "[";
    for (int j = 0; j < mat.C; j++) {
      os << mat[i][j] << ",";
    }
    os << "]" << endl;
  }
  return os;
}
template <typename T> struct Vector {
  Vector(const int n) : R(n), table(n, 0){};
  T &operator[](const int n) { return table[n]; }
  const T &operator[](const int n) const { return table[n]; }
  Vector &operator+=(const Vector &v) {
    assert(R == v.R);
    for (int i = 0; i < R; i++) {
      table[i] += v[i];
    }
    return *this;
  }
  Vector &operator-=(const Vector &v) {
    assert(R == v.R);
    for (int i = 0; i < R; i++) {
      table[i] -= v[i];
    }
    return *this;
  }
  Vector &operator*=(const T &s) {
    for (int i = 0; i < R; i++) {
      table[i] *= s;
    }
    return *this;
  }
  Vector &operator/=(const T &s) {
    assert(s != 0.0);
    for (int i = 0; i < R; i++) {
      table[i] /= s;
    }
    return *this;
  }
  Vector operator+(const Vector &v) const {
    assert(R == v.R);
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] + v[i];
    }
    return result;
  }
  Vector operator-(const Vector &v) const {
    assert(R == v.R);
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] - v[i];
    }
    return result;
  }
  Vector operator*(const T &s) const {
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] * s;
    }
    return result;
  }
  Vector operator/(const T &s) const {
    assert(s != 0.0);
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = table[i] / s;
    }
    return result;
  }
  Vector operator-() const {
    Vector result(R);
    for (int i = 0; i < R; i++) {
      result[i] = -table[i];
    }
    return result;
  }

  int R;
  vector<T> table;
};

template <typename T>
inline Vector<T> operator*(const T &s, const Vector<T> &v) {
  Vector<T> result(v.R);
  for (int i = 0; i < v.R; i++) {
    result[i] = s * v[i];
  }
  return result;
}

template <typename T>
inline Vector<T> operator*(const Matrix<T> &mat, const Vector<T> &v) {
  assert(mat.C == v.R);
  Vector<T> result(mat.R);
  for (int i = 0; i < v.R; i++) {
    for (int j = 0; j < mat.R; j++) {
      result[i] += mat[i][j] * v[j];
    }
  }
  return result;
}

template <typename T>
inline ostream &operator<<(ostream &os, const Vector<T> &v) {
  os << "Vec<" << v.R << ">" << endl;
  os << "[";
  for (int i = 0; i < v.R; i++) {
    os << v[i] << ",";
  }
  os << "]^T" << endl;
  return os;
}
template <typename T>
Vector<T> GaussJordan(const Matrix<T> &mat, const Vector<T> &v) {
  assert(mat.R == mat.C);
  const int N = mat.R;
  Matrix<T> A(N, N + 1);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      A[i][j] = mat[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    A[i][N] = v[i];
  }

  for (int i = 0; i < N; i++) {
    int pivot = i;
    for (int j = i; j < N; j++) {
      if (abs(A[j][i]) > abs(A[pivot][i])) {
        pivot = j;
      }
    }
    assert(A[pivot][i]);
    swap(A[i], A[pivot]);
    for (int j = i + 1; j <= N; j++) {
      A[i][j] /= A[i][i];
    }
    for (int j = 0; j < N; j++) {
      if (i != j) {
        for (int k = i + 1; k <= N; k++) {
          A[j][k] -= A[j][i] * A[i][k];
        }
      }
    }
  }
  Vector<T> res(N);
  for (int i = 0; i < N; i++) {
    res[i] = A[i][N];
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N;
    int S, T;
    cin >> S >> T;
    if (N == 0 and S == 0 and T == 0) {
      break;
    }
    S--, T--;
    vector<bool> q(N);
    for (int i = 0; i < N; i++) {
      int b;
      cin >> b;
      q[i] = b == 1;
    }
    vector<vector<ll>> d(N, vector<ll>(N, INF));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        ll D;
        cin >> D;
        d[i][j] = D == 0 ? INF : D;
      }
    }
    auto D = d;
    WarshallFloyd(d);
    if (d[S][T] == INF) {
      cout << "impossible" << endl;
      continue;
    }

    Matrix<ld> mat(N, N);
    Vector<ld> vec(N);
    for (int i = 0; i < N; i++) {
      if (i == T) {
        mat[i][T] = 1;
        vec[i] = 0;
        continue;
      }
      int dim = 0;
      ll sum = 0;
      if (q[i]) {
        for (int j = 0; j < N; j++) {
          if (D[i][j] != INF and d[i][T] == d[j][T] + D[i][j]) {
            dim++;
            sum += D[i][j];
            mat[i][j] = -1;
          }
        }
      } else {
        for (int j = 0; j < N; j++) {
          if (D[i][j] != INF) {
            dim++;
            sum += D[i][j];
            mat[i][j] = -1;
          }
        }
      }
      mat[i][i] = dim;
      vec[i] = sum;
    }
    cout << fixed << setprecision(15) << GaussJordan(mat, vec)[S] << endl;
  }
  return 0;
}