Pagini recente » Borderou de evaluare (job #120003) | Cod sursa (job #1941905) | Borderou de evaluare (job #2469884) | Borderou de evaluare (job #2865720) | Cod sursa (job #1871077)
#include <bits/stdc++.h>
using namespace std;
int m,n,i,x,y,cst,s[50010],nod,ap[50010],nr[50010];
vector<pair<int,int > >v[50010];
queue<int>q;
int main()
{
freopen("bellmanford.in","r",stdin);
freopen("bellmanford.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1; i<=m; ++i)
{
scanf("%d%d%d",&x,&y,&cst);
/// f>>x>>y>>cst;
v[x].push_back(make_pair(y,cst));
}
for(i=2; i<=n; ++i)
s[i]=20000000;
q.push(1);
nr[1]=1;
while(!q.empty())
{
nod=q.front();
q.pop();
ap[nod]=false;
for(auto &it:v[nod])
{
if(s[it.first]>s[nod]+it.second)
{
s[it.first]=s[nod]+it.second;
if(!ap[it.first])q.push(it.first),ap[it.first]=true;
nr[it.first]++;
if(nr[it.first]>=n)
{
printf("Ciclu negativ!\n");
return 0;
}
}
}
}
for(i=2; i<=n; ++i)
printf("%d ",s[i]);
return 0;
}