This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C" #include "../../template.h" #include "../floyd.h" void solve() { int n, m; cin >> n >> m; vector<vector<ll>> c(n, vector<ll> (n, INF)); for (int i = 0; i < n; i++) c[i][i] = 0; while (m--) { int u, v; ll cost; cin >> u >> v >> cost; c[u][v] = min(c[u][v], cost); } Floyd f(n, c); for (int i = 0; i < n; i++) { if (f.c[i][i] < 0) { cout << "NEGATIVE CYCLE" << endl; return; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (f.c[i][j] == INF) cout << "INF"; else cout << f.c[i][j]; cout << (j == n-1 ? '\n' : ' '); } } }
#line 1 "Graph/tests/aizu_grl_1_c_floyd.test.cpp" #define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C" #line 1 "template.h" #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; } #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; } #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; } #define sqr(x) ((x) * (x)) // For printing pair, container, etc. // Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator << (ostream& out, const Con& con) { out << '{'; for (auto beg = con.begin(), it = beg; it != con.end(); it++) { out << (it == beg ? "" : ", ") << *it; } return out << '}'; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) { return print_tuple_utils<0, tuple<U...>>(out, t); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long get_rand(long long r) { return uniform_int_distribution<long long> (0, r-1)(rng); } template<typename T> vector<T> read_vector(int n) { vector<T> res(n); for (int& x : res) cin >> x; return res; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; } #line 1 "Graph/floyd.h" // Tested: // - https://cses.fi/problemset/task/1672/ // - (trace) https://oj.vnoi.info/problem/floyd using ll = long long; const ll INF = 4e18; struct Floyd { Floyd(int _n, const std::vector<std::vector<ll>> _c) : n(_n), c(_c), trace(n) { for (int i = 0; i < n; i++) { trace[i] = std::vector<int> (n, -1); for (int j = 0; j < n; j++) { if (c[i][j] == INF) trace[i][j] = -1; else trace[i][j] = i; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (c[i][k] != INF && c[k][j] != INF && c[i][j] > c[i][k] + c[k][j]) { c[i][j] = c[i][k] + c[k][j]; trace[i][j] = trace[k][j]; } } } } } // Return {distance, path} // If no path -> returns {-1, {}} std::pair<ll, std::vector<int>> get_path(int start, int target) { if (trace[start][target] == -1) return {-1, {}}; std::vector<int> path; for (int u = target; u != start; u = trace[start][u]) { path.push_back(u); } path.push_back(start); reverse(path.begin(), path.end()); return {c[start][target], path}; } int n; std::vector<std::vector<ll>> c; std::vector<std::vector<int>> trace; }; #line 5 "Graph/tests/aizu_grl_1_c_floyd.test.cpp" void solve() { int n, m; cin >> n >> m; vector<vector<ll>> c(n, vector<ll> (n, INF)); for (int i = 0; i < n; i++) c[i][i] = 0; while (m--) { int u, v; ll cost; cin >> u >> v >> cost; c[u][v] = min(c[u][v], cost); } Floyd f(n, c); for (int i = 0; i < n; i++) { if (f.c[i][i] < 0) { cout << "NEGATIVE CYCLE" << endl; return; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (f.c[i][j] == INF) cout << "INF"; else cout << f.c[i][j]; cout << (j == n-1 ? '\n' : ' '); } } }