File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change 1
1
#include < limits.h>
2
+
2
3
#include < iostream>
4
+ #include < vector>
3
5
4
6
using namespace std ;
5
7
@@ -13,13 +15,13 @@ class Edge {
13
15
class Graph {
14
16
public:
15
17
int vertexNum, edgeNum;
16
- Edge * edges;
18
+ std::vector< Edge> edges;
17
19
18
20
// Constructs a graph with V vertices and E edges
19
21
Graph (int V, int E) {
20
22
this ->vertexNum = V;
21
23
this ->edgeNum = E;
22
- this ->edges = (Edge *) malloc (E * sizeof (Edge) );
24
+ this ->edges . reserve (E );
23
25
}
24
26
25
27
// Adds the given edge to the graph
@@ -36,7 +38,7 @@ class Graph {
36
38
};
37
39
38
40
// Utility function to print distances
39
- void print (int dist[] , int V) {
41
+ void print (const std::vector< int >& dist, int V) {
40
42
cout << " \n Vertex Distance" << endl;
41
43
for (int i = 0 ; i < V; i++) {
42
44
if (dist[i] != INT_MAX)
@@ -52,7 +54,8 @@ void print(int dist[], int V) {
52
54
void BellmanFord (Graph graph, int src) {
53
55
int V = graph.vertexNum ;
54
56
int E = graph.edgeNum ;
55
- int dist[V];
57
+ std::vector<int > dist;
58
+ dist.reserve (E);
56
59
57
60
// Initialize distances array as INF for all except source
58
61
// Intialize source as zero
You can’t perform that action at this time.
0 commit comments