Files
Algorithms-snippets/Graph Algorithm/Bellmann_Ford.cpp
2024-04-10 15:36:18 +05:30

19 lines
282 B
C++

vector<int> dist(n+1,(int)-1e17);
dist[1]=0;
for(int i = 1 ; i < n ; i++ ){
for(auto& e : g ){
int a,b,w;
tie(a,b,w)=e;
dist[b]=max(dist[b],dist[a]+w);
}
}
for(auto& e : g ){
int a,b,w;
tie(a,b,w)=e;
if( dist[b] < dist[a] + w ){
cycle[b]=true;
}
}