C++ Library for Competitive Programming
View the Project on GitHub emthrm/cp-library
/* * @title 数学/行列/連立一次方程式 * * verification-helper: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2171 * verification-helper: ERROR 1e-8 */ #include <iomanip> #include <iostream> #include <vector> #include "emthrm/graph/edge.hpp" #include "emthrm/graph/shortest_path/dijkstra.hpp" #include "emthrm/math/matrix/linear_equation.hpp" #include "emthrm/math/matrix/matrix.hpp" int main() { while (true) { int n, s, t; std::cin >> n >> s >> t; if (n == 0 && s == 0 && t == 0) break; --s; --t; std::vector<int> q(n); for (int i = 0; i < n; ++i) { std::cin >> q[i]; } std::vector<std::vector<emthrm::Edge<int>>> graph(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int a; std::cin >> a; if (a > 0) graph[i].emplace_back(i, j, a); } } emthrm::Dijkstra<int> dijkstra(graph); const std::vector<int> dist = dijkstra.build(t); if (dist[s] == dijkstra.inf) { std::cout << "impossible\n"; continue; } emthrm::Matrix<int> a(n, n, 0); std::vector<int> b(n, 0); for (int i = 0; i < n; ++i) { if (i == t) { a[i][i] = 1; } else { std::vector<emthrm::Edge<int>> edges; if (q[i] == 0) { edges = graph[i]; } else if (q[i] == 1) { for (const emthrm::Edge<int>& e : graph[i]) { if (dist[e.dst] + e.cost == dist[i]) edges.emplace_back(e); } } a[i][i] = -edges.size(); for (const emthrm::Edge<int>& e : edges) { ++a[i][e.dst]; b[i] -= e.cost; } } } std::cout << std::fixed << std::setprecision(8) << emthrm::linear_equation(a, b)[s] << '\n'; } return 0; }
#line 1 "test/math/matrix/linear_equation.test.cpp" /* * @title 数学/行列/連立一次方程式 * * verification-helper: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2171 * verification-helper: ERROR 1e-8 */ #include <iomanip> #include <iostream> #include <vector> #line 1 "include/emthrm/graph/edge.hpp" /** * @title 辺 */ #ifndef EMTHRM_GRAPH_EDGE_HPP_ #define EMTHRM_GRAPH_EDGE_HPP_ #include <compare> namespace emthrm { template <typename CostType> struct Edge { CostType cost; int src, dst; explicit Edge(const int src, const int dst, const CostType cost = 0) : cost(cost), src(src), dst(dst) {} auto operator<=>(const Edge& x) const = default; }; } // namespace emthrm #endif // EMTHRM_GRAPH_EDGE_HPP_ #line 1 "include/emthrm/graph/shortest_path/dijkstra.hpp" #include <algorithm> #include <cassert> #include <functional> #include <limits> #include <queue> #include <utility> #line 11 "include/emthrm/graph/shortest_path/dijkstra.hpp" #line 1 "include/emthrm/graph/edge.hpp" /** * @title 辺 */ #ifndef EMTHRM_GRAPH_EDGE_HPP_ #define EMTHRM_GRAPH_EDGE_HPP_ #include <compare> namespace emthrm { template <typename CostType> struct Edge { CostType cost; int src, dst; explicit Edge(const int src, const int dst, const CostType cost = 0) : cost(cost), src(src), dst(dst) {} auto operator<=>(const Edge& x) const = default; }; } // namespace emthrm #endif // EMTHRM_GRAPH_EDGE_HPP_ #line 13 "include/emthrm/graph/shortest_path/dijkstra.hpp" namespace emthrm { template <typename CostType> struct Dijkstra { const CostType inf; Dijkstra(const std::vector<std::vector<Edge<CostType>>>& graph, const CostType inf = std::numeric_limits<CostType>::max()) : inf(inf), is_built(false), graph(graph) {} std::vector<CostType> build(const int s) { is_built = true; const int n = graph.size(); std::vector<CostType> dist(n, inf); dist[s] = 0; prev.assign(n, -1); std::priority_queue<std::pair<CostType, int>, std::vector<std::pair<CostType, int>>, std::greater<std::pair<CostType, int>>> que; que.emplace(0, s); while (!que.empty()) { const auto [d, ver] = que.top(); que.pop(); if (d > dist[ver]) continue; for (const Edge<CostType>& e : graph[ver]) { if (dist[ver] + e.cost < dist[e.dst]) { dist[e.dst] = dist[ver] + e.cost; prev[e.dst] = ver; que.emplace(dist[e.dst], e.dst); } } } return dist; } std::vector<int> build_path(int t) const { assert(is_built); std::vector<int> res; for (; t != -1; t = prev[t]) { res.emplace_back(t); } std::reverse(res.begin(), res.end()); return res; } private: bool is_built; std::vector<int> prev; std::vector<std::vector<Edge<CostType>>> graph; }; } // namespace emthrm #line 1 "include/emthrm/math/matrix/linear_equation.hpp" #line 5 "include/emthrm/math/matrix/linear_equation.hpp" #include <cmath> #line 7 "include/emthrm/math/matrix/linear_equation.hpp" #line 1 "include/emthrm/math/matrix/gauss_jordan.hpp" #line 5 "include/emthrm/math/matrix/gauss_jordan.hpp" #line 1 "include/emthrm/math/matrix/matrix.hpp" #line 5 "include/emthrm/math/matrix/matrix.hpp" namespace emthrm { template <typename T> struct Matrix { explicit Matrix(const int m, const int n, const T def = 0) : data(m, std::vector<T>(n, def)) {} int nrow() const { return data.size(); } int ncol() const { return data.empty() ? 0 : data.front().size(); } Matrix pow(long long exponent) const { const int n = nrow(); Matrix<T> res(n, n, 0), tmp = *this; for (int i = 0; i < n; ++i) { res[i][i] = 1; } for (; exponent > 0; exponent >>= 1) { if (exponent & 1) res *= tmp; tmp *= tmp; } return res; } inline const std::vector<T>& operator[](const int i) const { return data[i]; } inline std::vector<T>& operator[](const int i) { return data[i]; } Matrix& operator=(const Matrix& x) = default; Matrix& operator+=(const Matrix& x) { const int m = nrow(), n = ncol(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { data[i][j] += x[i][j]; } } return *this; } Matrix& operator-=(const Matrix& x) { const int m = nrow(), n = ncol(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { data[i][j] -= x[i][j]; } } return *this; } Matrix& operator*=(const Matrix& x) { const int m = nrow(), l = ncol(), n = x.ncol(); std::vector<std::vector<T>> res(m, std::vector<T>(n, 0)); for (int i = 0; i < m; ++i) { for (int k = 0; k < l; ++k) { for (int j = 0; j < n; ++j) { res[i][j] += data[i][k] * x[k][j]; } } } data.swap(res); return *this; } Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; } Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; } Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; } private: std::vector<std::vector<T>> data; }; } // namespace emthrm #line 7 "include/emthrm/math/matrix/gauss_jordan.hpp" namespace emthrm { template <bool IS_EXTENDED = false, typename T> int gauss_jordan(Matrix<T>* a, const T eps = 1e-8) { const int m = a->nrow(), n = a->ncol(); int rank = 0; for (int col = 0; col < (IS_EXTENDED ? n - 1 : n); ++col) { int pivot = -1; T mx = eps; for (int row = rank; row < m; ++row) { const T abs = ((*a)[row][col] < 0 ? -(*a)[row][col] : (*a)[row][col]); if (abs > mx) { pivot = row; mx = abs; } } if (pivot == -1) continue; std::swap((*a)[rank], (*a)[pivot]); T tmp = (*a)[rank][col]; for (int col2 = 0; col2 < n; ++col2) { (*a)[rank][col2] /= tmp; } for (int row = 0; row < m; ++row) { if (row != rank && ((*a)[row][col] < 0 ? -(*a)[row][col] : (*a)[row][col]) > eps) { tmp = (*a)[row][col]; for (int col2 = 0; col2 < n; ++col2) { (*a)[row][col2] -= (*a)[rank][col2] * tmp; } } } ++rank; } return rank; } } // namespace emthrm #line 10 "include/emthrm/math/matrix/linear_equation.hpp" namespace emthrm { template <typename T, typename U = double> std::vector<U> linear_equation(const Matrix<T>& a, const std::vector<T>& b, const U eps = 1e-8) { const int m = a.nrow(), n = a.ncol(); Matrix<U> c(m, n + 1); for (int i = 0; i < m; ++i) { std::copy(a[i].begin(), a[i].end(), c[i].begin()); c[i].back() = b[i]; } const int rank = gauss_jordan<true>(&c, eps); for (int row = rank; row < m; ++row) { if ((c[row].back() < 0 ? -c[row].back() : c[row].back()) > eps) { return std::vector<U>{}; } } std::vector<U> res(n, 0); for (int i = 0, j = 0; i < rank; ++i) { while ((c[i][j] < 0 ? -c[i][j] : c[i][j]) <= eps) ++j; res[j++] = c[i].back(); } return res; } } // namespace emthrm #line 16 "test/math/matrix/linear_equation.test.cpp" int main() { while (true) { int n, s, t; std::cin >> n >> s >> t; if (n == 0 && s == 0 && t == 0) break; --s; --t; std::vector<int> q(n); for (int i = 0; i < n; ++i) { std::cin >> q[i]; } std::vector<std::vector<emthrm::Edge<int>>> graph(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int a; std::cin >> a; if (a > 0) graph[i].emplace_back(i, j, a); } } emthrm::Dijkstra<int> dijkstra(graph); const std::vector<int> dist = dijkstra.build(t); if (dist[s] == dijkstra.inf) { std::cout << "impossible\n"; continue; } emthrm::Matrix<int> a(n, n, 0); std::vector<int> b(n, 0); for (int i = 0; i < n; ++i) { if (i == t) { a[i][i] = 1; } else { std::vector<emthrm::Edge<int>> edges; if (q[i] == 0) { edges = graph[i]; } else if (q[i] == 1) { for (const emthrm::Edge<int>& e : graph[i]) { if (dist[e.dst] + e.cost == dist[i]) edges.emplace_back(e); } } a[i][i] = -edges.size(); for (const emthrm::Edge<int>& e : edges) { ++a[i][e.dst]; b[i] -= e.cost; } } } std::cout << std::fixed << std::setprecision(8) << emthrm::linear_equation(a, b)[s] << '\n'; } return 0; }