-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellman_ford.cpp
More file actions
31 lines (28 loc) · 819 Bytes
/
Copy pathbellman_ford.cpp
File metadata and controls
31 lines (28 loc) · 819 Bytes
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
#include <vector>
using namespace std;
struct edge{
int a,b,cost;
};
void bellman_ford(int ncount,const vector<edge>& edges,int source,
int *dist, bool *reachable, int *pred){
for(int i=0;i<ncount;i++){
dist[i]=-1;
reachable[i]=false;
pred[i] = -1;
}
dist[source]=0;
reachable[source]=true;
pred[source]=source;
for(int i=0;i<2*ncount;i++){
for(int j=0;j<edges.size();j++){
if(reachable[edges[j].a]){
int ncost = edges[j].cost+dist[edges[j].a];
if(!reachable[edges[j].b] || ncost < dist[edges[j].b]){
dist[edges[j].b]=ncost;
reachable[edges[j].b]=true;
pred[edges[j].b] = edges[j].a;
}
}
}
}
}