can somebody tell me what is wrong with my implementation of bellman ford algo.
Here’s my code
//Bellman-Ford algorithm
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
vector<int> dist;
vector<vector<int> > vec;
int n;
void bell_for(int src)
{
dist.resize(n+1, INT_MAX);
src--;
dist[src] = 0;
for(int i=1;i<n;i++)//run n-1 times
{
for(int j=0;j<n;j++)//for every edge joining j and k
for(int k=0;k<n;k++)
{
if(vec[j][k] == -1 || dist[j] == INT_MAX || k== src)
continue;
dist[k] = min(dist[k], dist[j] + vec[j][k]);
}
/*for(int h=0;h<n;h++)
cout << dist[h] << " ";
cout << endl; */
}
}
int main()
{
int m; cin >> n >> m;// m is no of edges|| n is no of vertices
vec.resize(n+1, vector<int> (n+1,-1));
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
{
vec[j][k] = -1;
}
for(int i=0;i<m;i++)
{
int v1,v2,weight;
cin >> v1 >> weight >> v2;
v1--; v2--;
vec[v1][v2] = weight;
vec[v2][v1] = weight;
}
/*for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
cout << vec[j][k] << " ";
cout << endl;
}
*/
int source;
cin >> source;
bell_for(source);
for(int i=0;i<n;i++)
cout << dist[i] << endl;
}
/*
8 10
1 4 2
1 -2 3
2 -1 3
3 1 4
4 1 5
5 6 6
4 -2 6
4 8 7
6 -4 7
7 2 8
*/