The problem is to find the shortest path from a given source vertex in a graph with weighted edges. The input consists of the graph G with vertices V and edges E, along with the source vertex r. The output should indicate if there is a negative cycle reachable from the source. If not, it should provide the cost of the shortest path from the source to each vertex. Constraints include the number of vertices and edges, edge weights, and the absence of parallel edges or self-loops. Sample inputs and outputs are provided to illustrate the problem.

# Buggy version:
import sys

def shortestPath(graph, s):
    n = len(graph)
    dist = [inf for _ in range(n)]
    dist[s] = 0

    for i in range(n):
        for u in range(n):
            for v, cost in graph[u]:
                newDist = dist[u] + cost
                if dist[u] != inf and newDist < dist[v]:
                    if i == n - 1:
                        return None
                    dist[v] = newDist
    return dist

def main():
    v, e, s = 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 = shortestPath(graph, s)

    if not dist:
        print("NEGATIVE CYCLE")
    else:
        for d in dist:
            if d >= inf:
                print("INF")
            else:
                print(d)
main()

# Fixed version:
import sys

inf = int(1e12)

def shortestPath(graph, s):
    n = len(graph)
    dist = [inf for _ in range(n)]
    dist[s] = 0

    for i in range(n):
        for u in range(n):
            for v, cost in graph[u]:
                newDist = dist[u] + cost
                if dist[u] != inf and newDist < dist[v]:
                    if i == n - 1:
                        return None
                    dist[v] = newDist
    return dist

def main():
    v, e, s = 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 = shortestPath(graph, s)

    if not dist:
        print("NEGATIVE CYCLE")
    else:
        for d in dist:
            if d >= inf:
                print("INF")
            else:
                print(d)
main()

# Buggy version:
class exist_negative_cycle(Exception):
    pass
def bellman_ford(g, size, start=0):
    d = [float('inf')] * size
    d[start] = 0
   
    for _ in range(size):
        for u in g:
            for length, v in g[u]:
                if d[v] > d[u] + length:
                    d[v] = d[u] + length

    for u in g:
        for length, v in g[u]:
            if d[v] > d[u] + length:
                raise exist_negative_cycle    
    return d
from sys import stdin
readline = stdin.readline

from collections import defaultdict

vertex, edge, r = map(int, readline().split())

g = defaultdict(list)
for i in range(edge):
    s, t, length = map(int, readline().split())
    g[s].append((length, t))
try:
    d = bellman_ford(g, vertex, r)
    for di in d:
        print('INF' if math.isinf(di) else di)
except exist_negative_cycle:
    print('NEGATIVE CYCLE')

# Fixed version:
class exist_negative_cycle(Exception):
    pass
def bellman_ford(g, size, start=0):
    d = [float('inf')] * size
    d[start] = 0
   
    for _ in range(size):
        for u in g:
            for length, v in g[u]:
                if d[v] > d[u] + length:
                    d[v] = d[u] + length

    for u in g:
        for length, v in g[u]:
            if d[v] > d[u] + length:
                raise exist_negative_cycle    
    return d
from sys import stdin
readline = stdin.readline
import math
from collections import defaultdict

vertex, edge, r = map(int, readline().split())

g = defaultdict(list)
for i in range(edge):
    s, t, length = map(int, readline().split())
    g[s].append((length, t))
try:
    d = bellman_ford(g, vertex, r)
    for di in d:
        print('INF' if math.isinf(di) else di)
except exist_negative_cycle:
    print('NEGATIVE CYCLE')

# Buggy version:
V, E, root = map(int, input().split())
visited = [False] * V
nodes = [[] for i in range(V)]
cost = [[] for i in range(V)]
for i in range(E):
    s, t, d = map(int, input().split())
    nodes[s].append(t)
    cost[s].append(d)

dist = [float('inf')] * V
dist[root] = 0

negative_cycle = False

for k in range(V):
    for i in range(V):
        for j in range(nodes[i]):
            if dist[nodes[i][j]] > dist[i] + cost[i][j]:
                dist[nodes[i][j]] = dist[i] + cost[i][j]
                if k==n-1:
                    dist[nodes[i][j]] = -float('inf')
                    negative_cycle = True
if negative_cycle:
    print('NEGATIVE CYCLE')
else:
    for i in range(V):
        if dist[i] == float('inf'):
            print('INF')
        else:
            print(dist[i])

# Fixed version:
V, E, root = map(int, input().split())
visited = [False] * V
nodes = [[] for i in range(V)]
cost = [[] for i in range(V)]
for i in range(E):
    s, t, d = map(int, input().split())
    nodes[s].append(t)
    cost[s].append(d)

dist = [float('inf')] * V
dist[root] = 0

negative_cycle = False

for k in range(V):
    for i in range(V):
        for j in range(len(nodes[i])):
            if dist[nodes[i][j]] > dist[i] + cost[i][j]:
                dist[nodes[i][j]] = dist[i] + cost[i][j]
                if k == V - 1:
                    dist[nodes[i][j]] = -float('inf')
                    negative_cycle = True
if negative_cycle:
    print('NEGATIVE CYCLE')
else:
    for i in range(V):
        if dist[i] == float('inf'):
            print('INF')
        else:
            print(dist[i])

# Buggy version:
nv, ne, r = map(int, input().split())

edges = set(tuple(map(int, input().split())) for _ in range(ne))
costs = [None] * nv
costs[r] = 0

for _ in range(nv - 1):
    for s, t, d in edges:
        cost = d if costs[s] is None else costs[s] + d
        if costs[t] is None or costs[t] > cost:
            costs[t] = cost

for s, t, d in edges:
    cost = d if costs[s] is None else costs[s] + d
    if cost < costs[t]:
        print('NEGATIVE CYCLE')
        break
else:
    for cost in costs:
        print('INF' if cost is None else cost)

# Fixed version:
nv, ne, r = map(int, input().split())

edges = set(tuple(map(int, input().split())) for _ in range(ne))
costs = [None] * nv
costs[r] = 0

for _ in range(nv - 1):
    for s, t, d in edges:
        if costs[s] is None:
            continue
        cost = costs[s] + d
        if costs[t] is None or costs[t] > cost:
            costs[t] = cost

for s, t, d in edges:
    if costs[s] is None:
        continue
    if costs[s] + d < costs[t]:
        print('NEGATIVE CYCLE')
        break
else:
    for cost in costs:
        print('INF' if cost is None else cost)

# Buggy version:
import sys

INF = float('inf')

def Bellmanford(n, edges, r):
    d = [INF] * n
    d[r] = 0
    
    for i in range(n):
        flag = False
        
        for (u, v, c) in edges:
            if d[u] != INF and d[u] + c < d[v]:
                d[v] = d[u] + c
                flag = True
                
        if flag and i == n-1:
            return 'NEGATIVE CYCLE'
        
    d = [di if di < INF else 'INF' for di in d]
    return d

V, E, R = map(int, sys.stdin.readline().split())
Edges = [None] * E

for i in range(E):
    si, ti, di = map(int, sys.stdin.readline().split())
    Edges[i] = (si, ti, di)
    
ans = Bellmanford(V, Edges, R)

print(*ans, sep = '\n')

# Fixed version:
import sys

INF = float('inf')

def Bellmanford(n, edges, r): #r: ?§????
    d = [INF] * n
    d[r] = 0
    
    for i in range(n):
        
        for (u, v, c) in edges:
            if d[u] != INF and d[u] + c < d[v]:
                d[v] = d[u] + c
                if i == n-1:
                    return ['NEGATIVE CYCLE']
    
    d = [di if di < INF else 'INF' for di in d]
    return d
    

V, E, R = map(int, sys.stdin.readline().split())
Edges = [None] * E

for i in range(E):
    si, ti, di = map(int, sys.stdin.readline().split())
    Edges[i] = (si, ti, di)
    
ans = Bellmanford(V, Edges, R)

print(*ans, sep = '\n')