-
Notifications
You must be signed in to change notification settings - Fork 0
/
floydWarshall.h
46 lines (46 loc) · 1.31 KB
/
floydWarshall.h
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
#pragma once
#include "graph.h"
vector<vector<double> > floydWarshall(graph G)
{
vector<vector<double> >dist(G.size(), vector<double>(G.size(), INF_EDGEWEIGHT));
for (int v = 0; v < G.size(); v++)
{
dist[v][v] = 0;
for (auto iter = G.adjList[v].begin(); iter != G.adjList[v].end(); iter++)
{
auto e = *iter;
dist[v][e.to] = e.weight;
}
}
for (int k = 0; k < G.size(); k++)
for (int i = 0; i < G.size(); i++)
for (int j = 0; j < G.size(); j++)
if (dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
return dist;
}
vector<vector<double> > floydWarshall(graph G, vector<vector<int> > &pre)
{
vector<vector<double> >dist(G.size(), vector<double>(G.size(), INF_EDGEWEIGHT));
pre = vector<vector<int> >(G.size(), vector<int>(G.size(), UNREACH_PATH));
for (int v = 0; v < G.size(); v++)
{
dist[v][v] = 0;
pre[v][v] = END_PATH;
for (auto iter = G.adjList[v].begin(); iter != G.adjList[v].end(); iter++)
{
auto e = *iter;
dist[v][e.to] = e.weight;
pre[v][e.to] = v;
}
}
for (int k = 0; k < G.size(); k++)
for (int i = 0; i < G.size(); i++)
for (int j = 0; j < G.size(); j++)
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
pre[i][j] = pre[k][j];
}
return dist;
}