Pagini recente » Cod sursa (job #2835185) | Cod sursa (job #592645) | Cod sursa (job #1645977) | Cod sursa (job #1886950) | Cod sursa (job #3277682)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
int n,m,i,j,a,b,x,y,c,cycles,ap[50004];
vector<pair<int,int>>v[50004];
int main()
{ f>>n>>m;
for (i=1;i<=m;i++){
f>>a>>b>>c;
v[a].push_back({b,c});
}
vector<int>d(n+2,250000005);
vector<int>cate(n+2,0);
queue<int>Q;
d[1]=0;
Q.push(1);
ap[1]=1;
while (!Q.empty()&&!cycles){
x=Q.front();
Q.pop();
ap[x]=0;
for (auto it:v[x]){
if (it.second+d[x]<d[it.first]){
d[it.first]=it.second+d[x];
cate[it.first]++;
if (cate[it.first]>=n){
cout<<"Ciclu negativ!\n";
goto cnt;
}
if (!ap[it.first]){
Q.push(it.first);
ap[it.first]=1;
}
}
}
}
for (i=2;i<=n;i++)cout<<d[i]<<' ';
cout<<'\n';
cnt:;
return 0;
}