MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / main

Function main

CPP/graph_tree/Bellman-Ford-Algorithm.cpp:14–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12};
13
14int main(){
15 int N=6,m=7;
16 vector<node> edges;
17 edges.push_back(node(0,1,5));
18 edges.push_back(node(1,2,-2));
19 edges.push_back(node(1,5,-3));
20 edges.push_back(node(2,4,3));
21 edges.push_back(node(3,2,6));
22 edges.push_back(node(3,4,-2));
23 edges.push_back(node(5,3,1));
24 int src=0;
25 int inf = 10000000;
26 vector<int> dist(N, inf);
27 dist[src] = 0;
28 for(int i = 1;i<=N-1;i++) {
29 for(auto it: edges) {
30 if(dist[it.u] + it.wt < dist[it.v]) {
31 dist[it.v] = dist[it.u] + it.wt;
32 }
33 }
34 }
35
36 int fl = 0;
37 for(auto it: edges) {
38 if(dist[it.u] + it.wt < dist[it.v]) {
39 cout << -1;
40 fl = 1;
41 break;
42 }
43 }
44
45 if(!fl) {
46 for(int i = 0;i<N;i++) {
47 cout << dist[i]<<" ";
48 }
49 }
50 return 0;
51}
52
53/*
54Output:

Callers

nothing calls this directly

Calls 2

push_backMethod · 0.80
nodeClass · 0.70

Tested by

no test coverage detected