-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloyd_warshall.cpp
64 lines (53 loc) · 1.18 KB
/
floyd_warshall.cpp
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
/*
Author: Andres Zibula
Github: https://github.com/andres-zibula/geekforgeeks-problems-solved
Problem link: http://practice.geeksforgeeks.org/problems/implementing-floyd-warshall/0
Description: The problem is to find shortest distances between every pair of
vertices in a given edge weighted directed Graph.
*/
#include <iostream>
using namespace std;
int main()
{
int T, V;
cin >> T;
for (int i = 0; i < T; ++i)
{
cin >> V;
int **graph = new int *[V];
for (int i = 0; i < V; ++i)
{
graph[i] = new int[V];
for (int j = 0; j < V; ++j)
{
cin >> graph[i][j];
}
}
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
for (int k = 0; k < V; ++k)
{
if (graph[j][k] > graph[j][i] + graph[i][k])
graph[j][k] = graph[j][i] + graph[i][k];
}
}
}
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
if (i == 0 && j == 0)
cout << graph[i][j];
else
cout << " " << graph[i][j];
}
}
cout << endl;
for (int i = 0; i < V; ++i)
delete[] graph[i];
delete[] graph;
}
return 0;
}