Pagini recente » Cod sursa (job #1160817) | Cod sursa (job #319199) | Cod sursa (job #1894638) | Cod sursa (job #3219188) | Cod sursa (job #2042716)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int const oo=1e8;
int main()
{
int n,m,x,y,c;
fin>>n>>m;
vector<vector<pair<int,int>>> path(n+5);
vector<int> dist(n+5,oo);
queue<int> q;
bitset<50010> in_queue;
vector<int> updated(n+5);
for(;m;m--)
{
fin>>x>>y>>c;
path[x].push_back(make_pair(y,c));
}
q.push(1);
dist[1]=0;
in_queue[1]=1;
updated[1]++;
int now;
while(q.size())
{
now=q.front();
q.pop();
in_queue[now]=0;
for(int i=0;i<path[now].size();i++)
{
tie(y,c)=path[now][i];
if(dist[y]>dist[now]+c)
{
updated[y]++;
dist[y]=dist[now]+c;
if(updated[y]>=n)
{
fout<<"Ciclu negativ!\n";
return 0;
}
q.push(y);
in_queue[y]=1;
}
}
}
for(int i=2;i<=n;i++)
fout<<dist[i]<<' ';
return 0;
}