forked from shivprime94/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellmen_ford.cpp
More file actions
89 lines (82 loc) · 1.6 KB
/
bellmen_ford.cpp
File metadata and controls
89 lines (82 loc) · 1.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*Given a graph and a source vertex src in graph,
find shortest paths from src to all vertices
in the given graph.
dijkstra doesn’t work for Graphs with negative
weight cycle, Bellman-Ford works for such
graphs. Bellman-Ford is also simpler than
Dijkstra and suites well for distributed systems.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
const long long max_n = 1LL<<40;
struct Edges{
int src,dtn,wgt;
}typedef(Edges);
struct Graph{
int v,e;
Edges *edge;
}typedef(Graph);
Graph *creategraph(int v,int e){
Graph *g=new Graph;
g->v=v;g->e=e;g->edge=new Edges[e];
return g;
}
void bellmenford(Graph *graph,int source){
int v=graph->v;int e=graph->e;long long dist[v];
for(int i=0;i<source;i++){
dist[i]=max_n;
}
dist[source]=0;
for(int i=0;i<=v-1;i++){
for(int j=0;j<e;j++){
int s=graph->edge[j].src;
int d=graph->edge[j].dtn;
int w=graph->edge[j].wgt;
if(dist[s]!=max_n && dist[s]+w<dist[d])
dist[d]=dist[s]+w;
}
}
cout<<"Vertex Distance from Source"<<endl;
for(int i=0;i<v;i++){
cout<<i<<"\t\t\t"<<dist[i]<<endl;
}
}
int main(){
// cout<<max_n<<endl;
int v,e,source;int x,y,z;
scanf("%d%d",&v,&e);
Graph *g=creategraph(v,e);
for(int i=0;i<e;i++){
cin>>x;cin>>y;cin>>z;
g->edge[i].src=x;g->edge[i].dtn=y;g->edge[i].wgt=z;
}
scanf("%d",&source);
bellmenford(g,source);
return 0;
}
/*
input--
5 8
0 1 -1
0 2 4
1 2 3
1 3 2
1 4 2
3 2 5
3 1 1
4 3 -3
0
*/
/*
output--
Vertex Distance from Source
0 0
1 -1
2 2
3 -2
4 1
*/