forked from No-Idle/graph-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFord_Bellman.cpp
39 lines (36 loc) · 960 Bytes
/
Ford_Bellman.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
#define int long long
struct Edge {
int u, v, weight;
friend istream &operator >> (istream &is, Edge &e) {
is >> e.u >> e.v >> e.weight;
--e.u, --e.v;
return is;
}
};
void Ford_Bellman(void) {
int n, m;
cin >> n >> m;
vector<int> dist(n, -1e18);
vector<int> prev(n);
vector<Edge> edges(m);
cin >> edges;
dist[0] = 0;
for (int i = 0; i < n - 1; ++i)
for (auto e : edges)
if (dist[e.v] < dist[e.u] + e.weight) {
dist[e.v] = dist[e.u] + e.weight;
prev[e.v]= e.u;
}
/// if we have not got negative cycles then all evaluated
Vertex *end = nullptr;
for (auto e : edges)
if (e.second->dist > e.first->dist + weight[e.first, e.second])
end = e.second;
for (int i = 0; i < n; ++i)
end = end->pred;
Vertex *p = end;
while (p != end) {
cout << p;
p = p->pred;
}
}