-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBellmanFordSP.cpp
35 lines (30 loc) · 942 Bytes
/
BellmanFordSP.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
#include "BellmanFordSP.h"
#include "DirectedCycle.h"
void BellmanFordSP::onRelaxationSuccess(const EdgeWeightedDigraph &G, int v, const DirectedEdge &e, int w) {
if (!onQ[w]) {
queue.push_back(w);
onQ[w] = true;
}
}
void BellmanFordSP::afterRelaxation(const EdgeWeightedDigraph &G, int v, const DirectedEdge &e, int w) {
if (cost++ % G.V() == 0) findNegativeCycle();
}
void BellmanFordSP::findNegativeCycle() {
int V = edgeTo.size();
EdgeWeightedDigraph spt(V);
for (int v = 0; v < V; ++v) {
if (edgeTo[v]) spt.addEdge(*edgeTo[v]);
}
DirectedCycle cf(spt);
cycle = cf.cycle();
}
BellmanFordSP::BellmanFordSP(const EdgeWeightedDigraph &G, int s) : SP(G, s), onQ(G.V()) {
queue.push_back(s);
onQ[s] = true;
while (!queue.empty() && !hasNegativeCycle()) {
int v = queue.front();
queue.pop_front();
onQ[v] = false;
relax(G, v);
}
}