Pagini recente » Cod sursa (job #3216635) | Cod sursa (job #1390083) | Cod sursa (job #1478875) | Cod sursa (job #1362557) | Cod sursa (job #2223252)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
const int N = 50010;
const int oo = 1000000007;
int n,m,x,y,c,d[N],cnt[N];
queue<int> Q;
vector<pair<int,int>> v[N];
bool p;
int main()
{
f>>n>>m;
for(;m;m--)
{
f>>x>>y>>c;
v[x].push_back(make_pair(y,c));
}
fill(d+2,d+n+1,oo);
Q.push(1);
while(Q.size())
{
x=Q.front();
for(auto it:v[x])
{
tie(y,c)=it;
if(d[y]>d[x]+c)
{
cnt[y]++;
if(cnt[y]>n)
{
g<<"Ciclu negativ!";
return 0;
}
d[y]=d[x]+c;
Q.push(y);
}
}
Q.pop();
}
for(int i=2;i<=n;i++)
g<<d[i]<<' ';
return 0;
}