bellman ford algorithm
//by YASH DHOKIA
//to find the single source shortest paths using Bellman-Ford algorithm
#include<bits/stdc++.h>
using namespace std;
class Edge {
	public:
		int src,des,weigth;
};
vector<int> bellman(Edge *edge,int v,int e) {
	vector<int> dist(v,INT_MAX);
	dist[0]=0;
	for(int i=0; i<v-1; i++) {
		for(int j=0; j<e; j++) {
			if(dist[edge[j].des]>dist[edge[j].src]+edge[j].weigth)
			        dist[edge[j].des]=dist[edge[j].src]+edge[j].weigth;
		}
	}
	for( int j=0; j<e; j++)
		if(dist[edge[j].des]>dist[edge[j].src]+edge[j].weigth)
		        cout<<"negative weigth cycle present in this graph";
	return dist;
}
int main() {
	int v,e;
	cin>>v>>e;
	Edge* edge=new Edge[e];
	for(int i=0; i<e; i++) {
		int s,d,w;
		cin>>s>>d>>w;
		edge[i].src=s;
		edge[i].des=d;
		edge[i].weigth=w;
	}
	vector<int> dist(v);
	dist=bellman(edge,v,e);
	for(int i=0; i<dist.size(); i++)
		cout<<i<<" "<<dist[i]<<" "<<endl;
		
	return 0;
}
/*
5 9
0 1 4
0 2 2
1 3 2
1 2 3
1 4 3
2 1 1
2 3 4
2 4 5
4 3 -5
*/
