-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloyd-Warshall.cs
executable file
·79 lines (69 loc) · 2.06 KB
/
Floyd-Warshall.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Floyd_Warshall
{
class Program
{
public static int[,] d;
public static int[,] pi;
public static void Inicializar(int[,] costo)
{
d = new int[costo.GetLength(0), costo.GetLength(1)];
pi = new int[costo.GetLength(0), costo.GetLength(1)];
for (int i = 0; i < costo.GetLength(0); i++)
{
for (int j = 0; j < costo.GetLength(1); j++)
{
if (costo[i, j] >= 0)
d[i, j] = costo[i, j];
else
d[i, j] = 100000;
}
}
for (int i = 0; i < d.GetLength(0); i++)
{
d[i, i] = 0;
}
}
public static void Floyd_Warshall(int[,] costo)
{
Inicializar(costo);
for (int k = 0; k < costo.GetLength(0); k++)
{
for (int i = 0; i < costo.GetLength(0); i++)
{
for (int j = 0; j < costo.GetLength(1); j++)
{
if (d[i, j] > d[i, k] + d[k, j])
d[i, j] = d[i, k] + d[k, j];
}
}
}
}
public static void Imprimir<T>(T[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write($"{matrix[i, j]} ");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
int[,] costo =
{
{ 0, 8, 5 },
{ 3, 0, -1 },
{ -1, 2, 0 }
};
Floyd_Warshall(costo);
Imprimir(d);
}
}
}