using System;
class GFG
{
static void BellmanFord(int [,]graph, int V,
int E, int src)
{
int []dis = new int[V];
for (int i = 0; i < V; i++)
dis[i] = int.MaxValue;
dis[src] = 0;
for (int i = 0; i < V - 1; i++)
{
for (int j = 0; j < E; j++)
{
if (dis[graph[j, 0]] = int.MaxValue && dis[graph[j, 0]] + graph[j, 2] <
dis[graph[j, 1]])
dis[graph[j, 1]] =
dis[graph[j, 0]] + graph[j, 2];
}
}
for (int i = 0; i < E; i++)
{
int x = graph[i, 0];
int y = graph[i, 1];
int weight = graph[i, 2];
if (dis[x] != int.MaxValue &&
dis[x] + weight < dis[y])
Console.WriteLine("Graph contains negative" +
" weight cycle");
}
Console.WriteLine("Vertex Distance from Source");
for (int i = 0; i < V; i++)
Console.WriteLine(i + "\t\t" + dis[i]);
}
public static void Main(String[] args)
{
int V = 5;
int E = 8;
int [,]graph = {{ 0, 1, -1 }, { 0, 2, 4 },
{ 1, 2, 3 }, { 1, 3, 2 },
{ 1, 4, 2 }, { 3, 2, 5 },
{ 3, 1, 1 }, { 4, 3, -3 }};
BellmanFord(graph, V, E, 0);
}
}