library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub komori3/library

:heavy_check_mark: test/verify/aoj-grl-1-c.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C"

#include "../../template/template.hpp"

#include "../../lib/warshall-floyd.hpp"

int main() {
    using std::cin, std::cout, std::endl, std::ios;
    cin.tie(0);
    ios::sync_with_stdio(false);

    constexpr ll inf = INT64_MAX / 8;

    int V, E;
    cin >> V >> E;

    auto G = make_vector<ll>(inf, V, V);
    for (int u = 0; u < V; u++) {
        G[u][u] = 0;
    }
    for (int e = 0; e < E; e++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        G[u][v] = w;
    }

    WarshallFloyd<ll> wf(inf, G);
    wf.build();

    if (wf.has_negative_loop()) {
        cout << "NEGATIVE CYCLE" << endl;
        return 0;
    }

    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            if (j) cout << ' ';
            if (wf.dist[i][j] == inf) cout << "INF";
            else cout << wf.dist[i][j];
        }
        cout << endl;
    }

    return 0;
}
#line 1 "test/verify/aoj-grl-1-c.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C"

#line 1 "template/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#line 5 "template/template.hpp"
#ifdef _MSC_VER
#include <ppl.h>
#else
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
/* const */
constexpr double PI = 3.141592653589793238462643;
/* io */
namespace aux {
    template<typename T, unsigned N, unsigned L> struct tp { static void print(std::ostream& os, const T& v) { os << std::get<N>(v) << ", "; tp<T, N + 1, L>::print(os, v); } };
    template<typename T, unsigned N> struct tp<T, N, N> { static void print(std::ostream& os, const T& v) { os << std::get<N>(v); } };
}
template<typename... Ts> std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t) { os << '['; aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::print(os, t); os << ']'; return os; }
template <class T, class = typename T::iterator, std::enable_if_t<!std::is_same<T, std::string>::value, int> = 0> std::ostream& operator<<(std::ostream& os, T const& a);
template <class T, class S> std::ostream& operator<<(std::ostream& os, std::pair<T, S> const& p) { return os << '[' << p.first << ", " << p.second << ']'; }
template <class T, class S> std::istream& operator>>(std::istream& is, std::pair<T, S>& p) { return is >> p.first >> p.second; }
template <class T, class, std::enable_if_t<!std::is_same<T, std::string>::value, int>>
std::ostream& operator<<(std::ostream& os, T const& a) { bool f = true; os << '['; for (auto const& x : a) { os << (f ? "" : ", ") << x; f = false; } os << ']'; return os; }
template <class T, size_t N, std::enable_if_t<!std::is_same<T, char>::value, int> = 0>
std::ostream& operator<<(std::ostream& os, const T(&a)[N]) { bool f = true; os << '['; for (auto const& x : a) { os << (f ? "" : ", ") << x; f = false; } os << ']'; return os; }
template <class T, class = decltype(std::begin(std::declval<T&>())), class = typename std::enable_if<!std::is_same<T, std::string>::value>::type>
std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; }
struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true);
/* format */
template<typename... Ts> std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector<char> b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); }
/* dump */
#define ENABLE_DUMP
#ifdef ENABLE_DUMP
#define DUMPOUT std::cerr
std::ostringstream DUMPBUF;
#define dump(...) do{DUMPBUF<<"  ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<"]"<<std::endl;DUMPBUF<<"    ";dump_func(__VA_ARGS__);DUMPOUT<<DUMPBUF.str();DUMPBUF.str("");DUMPBUF.clear();}while(0);
void dump_func() { DUMPBUF << std::endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); }
#else
#define dump(...) void(0);
#endif
struct Xorshift {
    uint64_t x = 88172645463325252LL;
    Xorshift(unsigned seed = 0) { set_seed(seed); }
    void set_seed(unsigned seed, int rep = 100) { x = (seed + 1) * 10007; for (int i = 0; i < rep; i++) next_int(); }
    unsigned next_int() { x = x ^ (x << 7); return x = x ^ (x >> 9); }
    unsigned next_int(unsigned mod) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % mod; }
    unsigned next_int(unsigned l, unsigned r) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % (r - l + 1) + l; }
    double next_double() { return double(next_int()) / UINT_MAX; }
} rnd;
template<typename T> void shuffle_vector(std::vector<T>& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } }
std::vector<std::string> split(std::string str, const std::string& delim) { for (char& c : str) if (delim.find(c) != std::string::npos) c = ' '; std::istringstream iss(str); std::vector<std::string> parsed; std::string buf; while (iss >> buf) parsed.push_back(buf); return parsed; }
template<typename T, typename ...Args> auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector<T>(arg, x); else return std::vector(arg, make_vector<T>(x, args...)); }
template<typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template<typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); }

using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using pdd = std::pair<double, double>;
#line 4 "test/verify/aoj-grl-1-c.test.cpp"

#line 1 "lib/warshall-floyd.hpp"
/**
 * @brief Warshall-Floyd (ワーシャルフロイド法, 全点対最短経路, All Pair Shortest Path, APSP)
 * @docs docs/warshall-floyd.md
 */
template<typename T>
struct WarshallFloyd {

    const T inf;
    const int V;
    std::vector<std::vector<T>> dist;
    std::vector<std::vector<int>> next;

    WarshallFloyd() = default;
    WarshallFloyd(const T& inf, const std::vector<std::vector<T>>& dist) : inf(inf), V((int)dist.size()), dist(dist), next(V, std::vector<int>(V)) {}
    
    void build() {
        for (int u = 0; u < V; u++) {
            dist[u][u] = 0;
        }
        for (int u = 0; u < V; u++) {
            for (int v = 0; v < V; v++) {
                next[u][v] = v;
            }
        }
        for (int k = 0; k < V; k++) {
            for (int i = 0; i < V; i++) {
                if (dist[i][k] == inf) continue;
                for (int j = 0; j < V; j++) {
                    if (dist[k][j] == inf) continue;
                    if (chmin(dist[i][j], dist[i][k] + dist[k][j])) {
                        next[i][j] = next[i][k];
                    }
                }
            }
        }
    }

    std::vector<int> get_path(int u, int v) const {
        std::vector<int> ret;
        for (int cur = u; cur != v; cur = next[cur][v]) {
            ret.push_back(cur);
        }
        ret.push_back(v);
        return ret;
    }

    bool has_negative_loop() const {
        for (int u = 0; u < V; u++) {
            if (dist[u][u] < 0) {
                return true;
            }
        }
        return false;
    }

};
#line 6 "test/verify/aoj-grl-1-c.test.cpp"

int main() {
    using std::cin, std::cout, std::endl, std::ios;
    cin.tie(0);
    ios::sync_with_stdio(false);

    constexpr ll inf = INT64_MAX / 8;

    int V, E;
    cin >> V >> E;

    auto G = make_vector<ll>(inf, V, V);
    for (int u = 0; u < V; u++) {
        G[u][u] = 0;
    }
    for (int e = 0; e < E; e++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        G[u][v] = w;
    }

    WarshallFloyd<ll> wf(inf, G);
    wf.build();

    if (wf.has_negative_loop()) {
        cout << "NEGATIVE CYCLE" << endl;
        return 0;
    }

    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            if (j) cout << ' ';
            if (wf.dist[i][j] == inf) cout << "INF";
            else cout << wf.dist[i][j];
        }
        cout << endl;
    }

    return 0;
}
Back to top page