-
Notifications
You must be signed in to change notification settings - Fork 0
/
Floyd_Warshall.cpp
107 lines (96 loc) · 2.39 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*************************************************************************
> File Name: Floyd_Warshall.cpp
> Author: Louis1992
> Mail: zhenchaogan@gmail.com
> Blog: http://gzc.github.io
> Created Time: Tue Nov 3 17:10:56 2015
************************************************************************/
#include<cstdio>
#include<vector>
#define V 5
#define INF 99999
using namespace std;
int dist[V][V];
int Pre[V][V];
void printSolution();
void printPre();
void floydWarshell (int graph[][V])
{
int i, j, k;
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
dist[i][j] = graph[i][j];
if(i == j || graph[i][j] == INF) Pre[i][j] = INF;
else Pre[i][j] = i+1;
}
}
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
Pre[i][j] = Pre[k][j];
}
}
}
}
printSolution();
printPre();
}
void printSolution()
{
printf ("Following matrix shows the shortest distances"
" between every pair of vertices \n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf ("%7d", dist[i][j]);
}
printf("\n");
}
}
void printPre()
{
printf ("Following matrix shows the Pre \n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (Pre[i][j] == INF)
printf("%7s", "INF");
else
printf ("%7d", Pre[i][j]);
}
printf("\n");
}
}
void findPath(int start, int end) {
vector<int> paths;
paths.push_back(end);
while(Pre[start-1][end-1] != start) {
paths.push_back(Pre[start-1][end-1]);
end = Pre[start-1][end-1];
}
paths.push_back(start);
printf("The path : ");
for(int i = paths.size()-1;i >= 0;i--)
printf("%i ", paths[i]);
printf("\n");
}
int main()
{
int graph[V][V] = {
{0, 3, 8, INF, -4},
{INF, 0, INF, 1, 7},
{INF, 4, 0, INF,INF},
{2, INF, -5, 0, INF},
{INF, INF, INF, 6, 0}
};
floydWarshell(graph);
findPath(2, 5);
return 0;
}