using System;
using System.Collections.Generic;
public class Solution
{
public void ShortestDist(List<List<int>> matrix)
{
int N = matrix.Count;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (matrix[i][j] == -1)
matrix[i][j] = int.MaxValue;
if (i == j)
matrix[i][j] = 0;
}
}
for (int k = 0; k < N; k++)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (matrix[i][k] != int.MaxValue && matrix[k][j] != int.MaxValue &&
matrix[i][k] + matrix[k][j] < matrix[i][j])
{
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (matrix[i][j] == int.MaxValue)
matrix[i][j] = -1;
}
}
}
}
public class Program
{
public static void Main()
{
List<List<int>> grid = new List<List<int>>
{
new List<int> { 0, 1, 2 }, new List<int> { 0, 3, 5 }, new List<int> { 0, 4, 3 },
new List<int> { 1, 0, 3 }, new List<int> { 1, 5, 6 }, new List<int> { 1, 2, 2 },
new List<int> { 1, 3, 2 }, new List<int> { 2, 5, 1 }, new List<int> { 2, 3, 1 },
new List<int> { 3, 4, 1 }, new List<int> { 4, 3, 2 }
};
int V = 6;
List<List<int>> matrix = new List<List<int>>(V);
for (int i = 0; i < V; i++)
{
matrix.Add(new List<int>(new int[V]));
for (int j = 0; j < V; j++)
{
matrix[i][j] = -1;
}
}
foreach (var it in grid)
{
int u = it[0];
int v = it[1];
int wt = it[2];
matrix[u][v] = wt;
}
Solution obj = new Solution();
obj.ShortestDist(matrix);
foreach (var row in matrix)
{
foreach (var value in row)
{
Console.Write($"{value}\t");
}
Console.WriteLine();
}
}
}