Pagini recente » Cod sursa (job #2717159) | Cod sursa (job #3033119) | Cod sursa (job #2082980) | Cod sursa (job #1042552) | Cod sursa (job #2601828)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("bellmanford.in");
ofstream cout("bellmanford.out");
int n ,m , x ,y ,z;
int cost[50001], viz[50001], fr[50001];
queue <int> q;
vector <pair<int,int> > L[50001];
int main() {
cin >> n >> m;
for(int i =1 ;i <=m ;i ++)
{
cin >> x >> y >> z;
L[x].push_back({y,z});
}
for(int i =1 ;i <=n ;i ++)
cost[i] = 1e9;
cost[1] = 0;
q.push(1);
fr[1] = 1;
while(!q.empty())
{
int aux = q.front();
q.pop();
for(auto i : L[aux])
{
if(cost[i.first] > cost[aux] + i.second)
{
cost[i.first] = cost[aux] + i.second;
q.push(i.first);
fr[i.first]++;
if(fr[i.first] >= n)
{
cout << "Ciclu negativ!";
return 0;
}
}
}
}
for(int i =2 ;i <=n; i ++)
cout << cost[i]<<" ";
return 0;
}