This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C"
#include <iostream>
#include "graph/warshall_floyd.hpp"
#include "graph/read_graph.hpp"
int main() {
{
const int n1 = 5, n2 = 5;
Graph<int> g(n1 + n2, true);
for (int i = 0; i < n1; i++) {
g.add_edge(i, (i + 1) % n1, -1, i);
}
for (int i = 0; i < n2; i++) {
g.add_edge(i + n1, (i + 1) % n2 + n1, -1, i + n1);
}
const int INF = 1 << 30;
auto dist = warshall_floyd(g, INF);
for (int i = 0; i < n1 + n2; i++) {
for (int j = 0; j < n1 + n2; j++) {
int same = (i < n1) == (j < n1);
if (same) {
assert(dist[i][j] == -INF);
} else {
assert(dist[i][j] == INF);
}
}
}
}
{
const int n1 = 5, n2 = 5;
Graph<int> g(n1 + n2, true);
for (int i = 0; i < n1; i++) {
g.add_edge(i, (i + 1) % n1, -1, i);
}
for (int i = 0; i < n2; i++) {
if (i % 2 == 0) {
g.add_edge(i + n1, i, 1, i + n1);
} else {
g.add_edge(i, i + n1, 1, i + n1);
}
}
const int INF = 1 << 30;
auto dist = warshall_floyd(g, INF);
for (int i = 0; i < n1 + n2; i++) {
for (int j = 0; j < n1 + n2; j++) {
if (i < n1 and j < n1) {
assert(dist[i][j] == -INF);
} else if (i < n1) {
assert(j >= n1);
if ((j - n1) % 2 == 0) {
assert(dist[i][j] == INF);
} else {
assert(dist[i][j] == -INF);
}
} else if (j < n1) {
assert(i >= n1);
if ((i - n1) % 2 == 0) {
assert(dist[i][j] == -INF);
} else {
assert(dist[i][j] == INF);
}
} else {
assert(i >= n1 and j >= n1);
if (i == j) {
assert(dist[i][j] == 0);
} else if ((i - n1) % 2 == (j - n1) % 2) {
assert(dist[i][j] == INF);
} else {
if ((i - n1) % 2 == 0) {
assert(dist[i][j] == -INF);
} else {
assert(dist[i][j] == INF);
}
}
}
}
}
}
int V, E;
std::cin >> V >> E;
auto g = read_graph<long long>(V, E, true, true, 0);
const long long INF = 1LL << 60;
auto dist = warshall_floyd(g, INF);
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++) {
if (dist[i][j] == -INF) {
std::cout << "NEGATIVE CYCLE\n";
return 0;
}
}
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF) {
std::cout << "INF";
} else {
std::cout << dist[i][j];
}
std::cout << " \n"[j + 1 == V];
}
}
return 0;
}
#line 1 "verify/graph/warshall_floyd.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C"
#include <iostream>
#line 2 "graph/warshall_floyd.hpp"
#line 2 "graph/graph_template.hpp"
#include <vector>
#include <cassert>
template <class T> struct Edge {
int from, to;
T cost;
int id;
Edge() = default;
Edge(const int from, const int to, const T cost = T(1), const int id = -1) : from(from), to(to), cost(cost), id(id) {}
friend bool operator<(const Edge<T>& a, const Edge<T>& b) { return a.cost < b.cost; }
friend std::ostream& operator<<(std::ostream& os, const Edge<T>& e) {
// output format: {id: cost(from, to) = cost}
return os << "{" << e.id << ": cost(" << e.from << ", " << e.to << ") = " << e.cost << "}";
}
};
template <class T> using Edges = std::vector<Edge<T>>;
template <class T> struct Graph {
struct EdgeIterators {
public:
using Iterator = typename std::vector<Edge<T>>::iterator;
EdgeIterators() = default;
EdgeIterators(const Iterator& begit, const Iterator& endit) : begit(begit), endit(endit) {}
Iterator begin() const { return begit; }
Iterator end() const { return endit; }
size_t size() const { return std::distance(begit, endit); }
Edge<T>& operator[](int i) const { return begit[i]; }
private:
Iterator begit, endit;
};
int n, m;
bool is_build, is_directed;
std::vector<Edge<T>> edges;
// CSR (Compressed Row Storage) 形式用
std::vector<int> start;
std::vector<Edge<T>> csr_edges;
Graph() = default;
Graph(const int n, const bool directed = false) : n(n), m(0), is_build(false), is_directed(directed), start(n + 1, 0) {}
// 辺を追加し, その辺が何番目に追加されたかを返す
int add_edge(const int from, const int to, const T cost = T(1), int id = -1) {
assert(!is_build);
assert(0 <= from and from < n);
assert(0 <= to and to < n);
if (id == -1) id = m;
edges.emplace_back(from, to, cost, id);
return m++;
}
// CSR 形式でグラフを構築
void build() {
assert(!is_build);
for (auto&& e : edges) {
start[e.from + 1]++;
if (!is_directed) start[e.to + 1]++;
}
for (int v = 0; v < n; v++) start[v + 1] += start[v];
auto counter = start;
csr_edges.resize(start.back() + 1);
for (auto&& e : edges) {
csr_edges[counter[e.from]++] = e;
if (!is_directed) csr_edges[counter[e.to]++] = Edge(e.to, e.from, e.cost, e.id);
}
is_build = true;
}
EdgeIterators operator[](int i) {
if (!is_build) build();
return EdgeIterators(csr_edges.begin() + start[i], csr_edges.begin() + start[i + 1]);
}
size_t size() const { return (size_t)(n); }
friend std::ostream& operator<<(std::ostream& os, Graph<T>& g) {
os << "[";
for (int i = 0; i < (int)(g.size()); i++) {
os << "[";
for (int j = 0; j < (int)(g[i].size()); j++) {
os << g[i][j];
if (j + 1 != (int)(g[i].size())) os << ", ";
}
os << "]";
if (i + 1 != (int)(g.size())) os << ", ";
}
return os << "]";
}
};
#line 4 "graph/warshall_floyd.hpp"
template <class T> std::vector<std::vector<T>> warshall_floyd(Graph<T>& g, const T inf) {
const int n = (int)(g.size());
std::vector dist(n, std::vector<T>(n, inf));
for (int i = 0; i < n; i++) {
dist[i][i] = 0;
for (auto&& e : g[i]) {
dist[e.from][e.to] = std::min(dist[e.from][e.to], e.cost);
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
if (dist[i][k] == inf) continue;
for (int j = 0; j < n; j++) {
if (dist[k][j] == inf) continue;
dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
// 負閉路検出
for (int i = 0; i < n; i++) {
if (dist[i][i] < 0) dist[i][i] = -inf;
}
// 負閉路をもとに小さくできるパスについて調べる
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
if (dist[i][k] == inf) continue;
for (int j = 0; j < n; j++) {
if (dist[k][j] == inf) continue;
T nd = dist[i][k] + dist[k][j];
if (dist[i][k] == -inf or dist[k][j] == -inf) nd = -inf;
dist[i][j] = std::min(dist[i][j], nd);
}
}
}
return dist;
}
#line 2 "graph/read_graph.hpp"
#line 4 "graph/read_graph.hpp"
template <class T> Graph<T> read_graph(const int n, const int m, const bool weight = false, const bool directed = false, const int offset = 1) {
Graph<T> g(n, directed);
for (int i = 0; i < m; i++) {
int a, b;
std::cin >> a >> b;
a -= offset, b -= offset;
T c = 1;
if (weight) std::cin >> c;
g.add_edge(a, b, c);
}
g.build();
return g;
}
template <class T> Graph<T> read_parent(const int n, const bool weight = false, const bool directed = false, const int offset = 1) {
Graph<T> g(n, directed);
for (int i = 1; i < n; i++) {
int p;
std::cin >> p;
p -= offset;
T c = 1;
if (weight) std::cin >> c;
g.add_edge(p, i, c);
}
g.build();
return g;
}
#line 7 "verify/graph/warshall_floyd.test.cpp"
int main() {
{
const int n1 = 5, n2 = 5;
Graph<int> g(n1 + n2, true);
for (int i = 0; i < n1; i++) {
g.add_edge(i, (i + 1) % n1, -1, i);
}
for (int i = 0; i < n2; i++) {
g.add_edge(i + n1, (i + 1) % n2 + n1, -1, i + n1);
}
const int INF = 1 << 30;
auto dist = warshall_floyd(g, INF);
for (int i = 0; i < n1 + n2; i++) {
for (int j = 0; j < n1 + n2; j++) {
int same = (i < n1) == (j < n1);
if (same) {
assert(dist[i][j] == -INF);
} else {
assert(dist[i][j] == INF);
}
}
}
}
{
const int n1 = 5, n2 = 5;
Graph<int> g(n1 + n2, true);
for (int i = 0; i < n1; i++) {
g.add_edge(i, (i + 1) % n1, -1, i);
}
for (int i = 0; i < n2; i++) {
if (i % 2 == 0) {
g.add_edge(i + n1, i, 1, i + n1);
} else {
g.add_edge(i, i + n1, 1, i + n1);
}
}
const int INF = 1 << 30;
auto dist = warshall_floyd(g, INF);
for (int i = 0; i < n1 + n2; i++) {
for (int j = 0; j < n1 + n2; j++) {
if (i < n1 and j < n1) {
assert(dist[i][j] == -INF);
} else if (i < n1) {
assert(j >= n1);
if ((j - n1) % 2 == 0) {
assert(dist[i][j] == INF);
} else {
assert(dist[i][j] == -INF);
}
} else if (j < n1) {
assert(i >= n1);
if ((i - n1) % 2 == 0) {
assert(dist[i][j] == -INF);
} else {
assert(dist[i][j] == INF);
}
} else {
assert(i >= n1 and j >= n1);
if (i == j) {
assert(dist[i][j] == 0);
} else if ((i - n1) % 2 == (j - n1) % 2) {
assert(dist[i][j] == INF);
} else {
if ((i - n1) % 2 == 0) {
assert(dist[i][j] == -INF);
} else {
assert(dist[i][j] == INF);
}
}
}
}
}
}
int V, E;
std::cin >> V >> E;
auto g = read_graph<long long>(V, E, true, true, 0);
const long long INF = 1LL << 60;
auto dist = warshall_floyd(g, INF);
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++) {
if (dist[i][j] == -INF) {
std::cout << "NEGATIVE CYCLE\n";
return 0;
}
}
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF) {
std::cout << "INF";
} else {
std::cout << dist[i][j];
}
std::cout << " \n"[j + 1 == V];
}
}
return 0;
}