This repository was archived by the owner on Nov 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathfloyd_warshall.cpp
More file actions
91 lines (78 loc) · 1.55 KB
/
floyd_warshall.cpp
File metadata and controls
91 lines (78 loc) · 1.55 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Program to find the shortest path between all pairs of vertices in a
* directed graph using the Floyd-Warshall algorithm.
*/
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
const int inf = 1e9;
int** floyd_warshall(int **g, int n);
int main()
{
int n;
// Number of vertices in the graph.
cin >> n;
int m;
// Number of edges.
cin >> m;
int **g = new int*[n];
for (int i = 0; i < n; i++)
{
g[i] = new int[n];
memset(g[i], 0, n*sizeof(int));
}
// Adjacency matrix representation of the graph.
for (int i = 0; i < n; i++)
{
int u, v, w;
// u, v are the end-points of the edge, 0-indexed.
// w is the weight of the edge.
// 0 <= u, v < n; w != 0
cin >> u >> v >> w;
g[u][v] = w;
}
// Storing distance matrix in "dist".
int **dist = floyd_warshall(g, n);
// Printing distance matrix.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << setw(5) << dist[i][j];
cout << "\n";
}
return 0;
}
int** floyd_warshall(int **g, int n)
{
int **dist = new int*[n];
for (int i = 0; i < n; i++)
{
dist[i] = new int[n];
memset(dist[i], 0, n*sizeof(int));
}
// Initialising distance matrix.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
dist[i][j] = inf;
if (g[i][j])
dist[i][j] = g[i][j];
}
dist[i][i] = 0;
}
// Finding shortest paths.
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
return dist;
}