Title: All Pairs Shortest Path

Input:
An edge-weighted graph G (V, E), where |V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output:
If the graph contains a negative cycle, print "NEGATIVE CYCLE" in a line. Otherwise, print the cost of the shortest path from vertex i to each vertex j. If there is no path from vertex i to vertex j, print "INF". The output consists of |V| lines.

Constraints:
- 1 ≤ |V| ≤ 100
- 0 ≤ |E| ≤ 9900
- -2 × 10^7 ≤ di ≤ 2 × 10^7
- There are no parallel edges
- There are no self-loops

Sample Input 1:
4 6
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1
3 2 7

Sample Output 1:
0 1 3 4
INF 0 2 3
INF INF 0 1
INF INF 7 0

Sample Input 2:
4 6
0 1 1
0 2 -5
1 2 2
1 3 4
2 3 1
3 2 7

Sample Output 2:
0 1 -5 -4
INF 0 2 3
INF INF 0 1
INF INF 7 0

Sample Input 3:
4 6
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1
3 2 -7

Sample Output 3:
NEGATIVE CYCLE

# Buggy version:
from sys import stdin
from collections import deque

def read_graph(v, e):
    A = [ [float('inf')]*v for _ in range(v) ]
    for i in range(v):
        A[i][i] = 0
    for _ in range(e):
        line = list(stdin.readline().strip().split())
        i = line[0]
        j = line[1]
        k = line[2]
        A[int(i)][int(j)] = int(k)
    return A

def worshall_floyd(A, v):
    for k in range(v):
        for i in range(v):
            if A[i][k] == float('inf'):
                continue
            for j in range(v):
                if A[k][j] == float('inf'):
                    continue
                A[i][j] = min(A[i][j], A[i][k] + A[k][j])
    return A

def print_ans(A):
    if min(min(A)) < 0:
        print('NEGATIVE CYCLE')
    else:
        for i in A:
            print(*[str(k).upper() for k in i], sep=' ')

v, e = [int(i) for i in input().split()]
A = read_graph(v, e)
print_ans(worshall_floyd(A, v))


# Fixed version:
from sys import stdin
from collections import deque

def read_graph(v, e):
    A = [ [float('inf')]*v for _ in range(v) ]
    for i in range(v):
        A[i][i] = 0
    for _ in range(e):
        line = list(stdin.readline().strip().split())
        i = line[0]
        j = line[1]
        k = line[2]
        A[int(i)][int(j)] = int(k)
    return A

def worshall_floyd(A, v):
    for k in range(v):
        for i in range(v):
            if A[i][k] == float('inf'):
                continue
            for j in range(v):
                if A[k][j] == float('inf'):
                    continue
                A[i][j] = min(A[i][j], A[i][k] + A[k][j])
    return A

def print_ans(A, v):
    NEGATIVE = False
    for i in range(v):
        if A[i][i] < 0:
            NEGATIVE = True
    if NEGATIVE:
        print('NEGATIVE CYCLE')
    else:
        for i in A:
            print(*[str(k).upper() for k in i], sep=' ')

v, e = [int(i) for i in input().split()]
A = read_graph(v, e)
A = worshall_floyd(A, v)
print_ans(A, v)

# Buggy version:
from sys import exit
INF = 1<<21
V,E = [int(n) for n in input().split()]
s = [0]*E
t = [0]*E
d = [0]*E
dist = [[INF for _ in range(V)] for __ in range(V)]
for i in range(V):
    dist[i][i]= 0
for i in range(E):
    s[i], t[i], d[i] = [int(n) for n in input().split()]
    dist[s[i]][t[i]] = d[i]

for k in range(V):
    for i in range(V):
        if(dist[i][k]== INF):continue
        for j in range(V):
            if(dist[k][j]== INF):continue
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

for i in range(V):
    for j in range(V):
        print(dist[i][j] if dist[i][j] != INF else "INF",end=" ")
    print()

# N = int(input())
# a = [int(input()) for _ in range(N)]
# S = str(input())
# L = len(S)
# T = str(input())
# exit()



# Fixed version:
from sys import exit
INF = 1<<32
V,E = [int(n) for n in input().split()]
s = [0]*E
t = [0]*E
d = [0]*E
dist = [[INF for _ in range(V)] for __ in range(V)]
for i in range(V):
    dist[i][i]= 0
for i in range(E):
    s[i], t[i], d[i] = [int(n) for n in input().split()]
    dist[s[i]][t[i]] = d[i]

for k in range(V):
    for i in range(V):
        if(dist[i][k]== INF):continue
        for j in range(V):
            if(dist[k][j]== INF):continue
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

for i in range(V):
    if dist[i][i] < 0:
        print("NEGATIVE CYCLE")
        exit()

for i in range(V):
    for j in range(V):
        if j != V-1:
            print(dist[i][j] if dist[i][j] != INF else "INF",end=" ")
        else:
            print(dist[i][j] if dist[i][j] != INF else "INF")

# N = int(input())
# a = [int(input()) for _ in range(N)]
# S = str(input())
# L = len(S)
# T = str(input())
# exit()

# Buggy version:
import copy

INF = 10**10
v, e = map(int, input().split())
M = [[INF]*v for i in range(v)]
for i in range(v):
    M[i][i] = 0
for i in range(e):
    s, t, d = map(int, input().split())
    M[s][t] = d

# k:グラフの範囲
ans = copy.deepcopy(M)
for k in range(v):
    for i in range(v):
        for j in range(v):
            # i, jのグラフの最短距離
            if k == 0:
                ans[i][j] = M[i][j]
            else:
                ans[i][j] = min(ans[i][j], ans[i][k]+ans[k][j])

flag = False
for line in ans:
    if any([i < 0 for i in line]):
        flag = True
        break
if flag:
    print('NEGATIVE CYCLE')
else:
    for line in ans:
        line = ['INF' if tmp == INF else tmp for tmp in line]
        print(*line)



# Fixed version:
import copy

INF = 10**10
v, e = map(int, input().split())
M = [[INF]*v for i in range(v)]
for i in range(v):
    M[i][i] = 0
for i in range(e):
    s, t, d = map(int, input().split())
    M[s][t] = d

# k:グラフの範囲
ans = copy.deepcopy(M)
for k in range(v):
    for i in range(v):
        if ans[i][k] == INF:
            continue
        for j in range(v):
            if ans[k][j] == INF:
                continue
            # i, jのグラフの最短距離
            ans[i][j] = min(ans[i][j], ans[i][k]+ans[k][j])

flag = False
for i in range(v):
    if ans[i][i] < 0:
        flag = True
        break
if flag:
    print('NEGATIVE CYCLE')
else:
    for line in ans:
        line = ['INF' if tmp == INF else tmp for tmp in line]
        print(*line)

# Buggy version:
import sys

inf = int(1e12)

def shortestPath(graph):
    n = len(graph)
    dist = [[inf for _ in range(n)] for _ in range(n)]
    for i in range(n):
        dist[i][i] = 0
    
    for v in range(n):
        for u, cost in graph[v]:
            dist[v][u] = cost

    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
                
    return dist

def main():
    v, e = map(int, input().split())
    
    graph = [[] for _ in range(v)]
    
    for i in range(e):
        a, b, cost = map(int, input().split())
        graph[a].append((b, cost))
            
    dist_mat = shortestPath(graph)

    for i in range(v):
        output = []
        for j in range(v):
            if dist_mat[i][j] >= inf:
                output.append("INF")
            else:
                output.append(str(dist_mat[i][j]))
        print(" ".join(output))

main()

# Fixed version:
import sys

inf = int(1e12)

def shortestPath(graph):
    n = len(graph)
    dist = [[inf for _ in range(n)] for _ in range(n)]
    for i in range(n):
        dist[i][i] = 0
    
    for v in range(n):
        for u, cost in graph[v]:
            dist[v][u] = cost

    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dist[i][k] != inf and dist[k][j] != inf:
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
                
    return dist

def main():
    v, e = map(int, input().split())
    
    graph = [[] for _ in range(v)]
    
    for i in range(e):
        a, b, cost = map(int, input().split())
        graph[a].append((b, cost))
            
    dist_mat = shortestPath(graph)

    for i in range(v):
        if dist_mat[i][i] < 0:
            print("NEGATIVE CYCLE")
            return

    for i in range(v):
        output = []
        for j in range(v):
            if dist_mat[i][j] >= inf:
                output.append("INF")
            else:
                output.append(str(dist_mat[i][j]))
        print(" ".join(output))

main()

# Buggy version:
nv, ne = map(int, input().split(' '))
edges = []
for i in range(ne):
    s, t, w = map(int, input().split(' '))
    edges.append((s, t, w))

dist = [[float('inf')] * nv for i in range(nv)]
for i in range(nv):
    dist[i][i] = 0
for s, t, w in edges:
    dist[s][t] = w

for k in range(nv):
    for i in range(nv):
        for j in range(nv):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]

for i in range(nv):
    if dist[i][i] < 0:
        print("NEGATIVE CYCLE")
        exit()

[print(' '.join(map(str, dist[i]))) for i in range(nv)]

# Fixed version:
nv, ne = map(int, input().split(' '))
edges = []
for i in range(ne):
    s, t, w = map(int, input().split(' '))
    edges.append((s, t, w))

dist = [[float('inf')] * nv for i in range(nv)]
for i in range(nv):
    dist[i][i] = 0
for s, t, w in edges:
    dist[s][t] = w

for k in range(nv):
    for i in range(nv):
        for j in range(nv):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]

for i in range(nv):
    if dist[i][i] < 0:
        print("NEGATIVE CYCLE")
        exit()

for row in dist:
    print(' '.join(str(d) if d < float('inf') else 'INF' for d in row))

# Buggy version:
from collections import defaultdict

vertice,edge = map(int,input().split(" "))
cost = [[0 if m == n else float("inf") for n in range(vertice)] for m in range(vertice)]
link = defaultdict(dict)
weight = defaultdict(dict)

for e in range(edge):
    i,j,w = map(int,input().split(""))
    link[i] = link.get(i,set()) | {j}
    weight[i].update({j:w})

vertice_cnt = 0
while 1:
    if vertice_cnt >= vertice:
        print("NEGATIVE CYCLE")
        break
    update = False
    for source in range(vertice):
        for k,value in link.items():
            for v in value:
                if cost[source][v] > cost[source][k] + weight[k][v]:
                    cost[source][v] = cost[source][k] + weight[k][v]
                    update = True
    if not update:
        for o in range(vertice):

            print(*([n if n != float("INF") else "INF" for n in cost[o]]))
        break
    vertice_cnt += 1

# Fixed version:
from collections import defaultdict

vertice,edge = map(int,input().split(" "))
cost = [[0 if m == n else float("inf") for n in range(vertice)] for m in range(vertice)]
link = defaultdict(dict)
weight = defaultdict(dict)

for e in range(edge):
    i,j,w = map(int,input().split(" "))
    link[i] = link.get(i,set()) | {j}
    weight[i].update({j:w})

vertice_cnt = 0
while 1:
    if vertice_cnt >= vertice:
        print("NEGATIVE CYCLE")
        break
    update = False
    for source in range(vertice):
        for k,value in link.items():
            for v in value:
                if cost[source][v] > cost[source][k] + weight[k][v]:
                    cost[source][v] = cost[source][k] + weight[k][v]
                    update = True
    if not update:
        for o in range(vertice):

            print(*([n if n != float("INF") else "INF" for n in cost[o]]))
        break
    vertice_cnt += 1

# Buggy version:
from collections import defaultdict
vertices, edges = (int(n) for n in input().split(" "))
connect = defaultdict(list)
cost_dp = [[float("inf") if n != m else 0 for n in range(vertices)] for m in range(vertices)]
for e in range(edges):
    v1, v2, weight = (int(n) for n in input().split(" "))
    connect[v1].append([v2,weight])
    connect[v2].append([v1,weight])
    cost_dp[v1][v2] = weight
for neg in range(2):
    updated = 0
    for t in range(vertices):
        for i in range(vertices):
            for k in range(vertices):
                if cost_dp[i][k] > cost_dp[i][t] + cost_dp[t][k]:
                    updated = 1
                    cost_dp[i][k] = cost_dp[i][t] + cost_dp[t][k]
if neg == 1 and updated:
    print("NEGATIVE CYCLE")
else:
    for ve in range(vertices):
        ans = ["INF" if n == float("inf") else n for n in cost_dp[ve]]
        print(*ans)
    break

# Fixed version:
from collections import defaultdict
vertices, edges = (int(n) for n in input().split(" "))
connect = defaultdict(list)
cost_dp = [[float("inf") if n != m else 0 for n in range(vertices)] for m in range(vertices)]
for e in range(edges):
    v1, v2, weight = (int(n) for n in input().split(" "))
    connect[v1].append([v2,weight])
    connect[v2].append([v1,weight])
    cost_dp[v1][v2] = weight
for neg in range(2):
    updated = 0
    for t in range(vertices):
        for i in range(vertices):
            for k in range(vertices):
                if cost_dp[i][k] > cost_dp[i][t] + cost_dp[t][k]:
                    updated = 1
                    cost_dp[i][k] = cost_dp[i][t] + cost_dp[t][k]
if neg == 1 and updated:
    print("NEGATIVE CYCLE")
else:
    for ve in range(vertices):
        ans = ["INF" if n == float("inf") else n for n in cost_dp[ve]]
        print(*ans)

# Buggy version:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
 
INF = float("inf")
 
V, E = inpl()
adj = [[] for _ in range(V)]
D = [[INF]*V for _ in range(V)]
 
for _ in range(E):
    s, t, d = inpl()
    D[s][t] = d
 
for v in range(V):
    D[v][v] = 0 

def WarshallFloyd(G):
    V = len(G)
    for k in range(V):
        for i in range(V):
            for j in range(V):
                G[i][j] = min(G[i][j], G[i][k] + G[k][j])
    return G

D = WarshallFloyd(D)

for v in range(V):
    if D[v][v] < 0:
        print("NEGATIVE CYCE")
        break
else:
    for d in range(V):
        print(" ".join(map(str, D[d])).replace("inf", "INF"))


# Fixed version:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
 
INF = float("inf")
 
V, E = inpl()
adj = [[] for _ in range(V)]
D = [[INF]*V for _ in range(V)]
 
for _ in range(E):
    s, t, d = inpl()
    D[s][t] = d
 
for v in range(V):
    D[v][v] = 0 

def WarshallFloyd(G):
    V = len(G)
    for k in range(V):
        for i in range(V):
            for j in range(V):
                G[i][j] = min(G[i][j], G[i][k] + G[k][j])
    return G

D = WarshallFloyd(D)

for v in range(V):
    if D[v][v] < 0:
        print("NEGATIVE CYCLE")
        break
else:
    for d in range(V):
        print(" ".join(map(str, D[d])).replace("inf", "INF"))

# Buggy version:
import queue
import math


def warshall(graph, n):
    for i in range(n):
        graph[i][i] = 0

    for k in range(n):
        for i in range(n):
            for j in range(n):
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

    return graph


def main():
    n, m = map(int, input().split())
    graph = [[float('inf') for _ in range(n)] for _ in range(n)]

    for _ in range(m):
        data = list(map(int, input().split()))

        graph[data[0]][data[1]] = data[2]

    dist = warshall(graph, n)

    for i in range(n):
        if dist[i][i] < 0:
            print("NEGATIVE CYCLE")
            return

    for y in dist:
        for x in y:
            print('{} '.format(x if x != float('inf') else 'INF'), end="")
        print()

    return


if __name__ == '__main__':
    main()



# Fixed version:
import queue
import math


def warshall(graph, n):
    for i in range(n):
        graph[i][i] = 0

    for k in range(n):
        for i in range(n):
            for j in range(n):
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

    return graph


def main():
    n, m = map(int, input().split())
    graph = [[float('inf') for _ in range(n)] for _ in range(n)]

    for _ in range(m):
        data = list(map(int, input().split()))

        graph[data[0]][data[1]] = data[2]

    dist = warshall(graph, n)

    for i in range(n):
        if dist[i][i] < 0:
            print("NEGATIVE CYCLE")
            return

    for i in range(n):
        for j in range(n):

            if j == 0:
                print(dist[i][j] if dist[i][j] != float('inf') else 'INF', end="")
            else:
                print(' {}'.format(dist[i][j] if dist[i][j] != float('inf') else 'INF'), end="")

        print()

    return


if __name__ == '__main__':
    main()