Pagini recente » Cod sursa (job #690974) | Cod sursa (job #2812758) | Cod sursa (job #2333644) | Cod sursa (job #1858917) | Cod sursa (job #1853972)
#include <iostream>
#include <cstdio>
#include <vector>
#include <fstream>
#include <queue>
#include <climits>
#define Nmax 50001
#define Mmax 250001
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
int n, m, cost[Nmax], act[Nmax];
vector < pair<int, int> > gr[Mmax];
queue <int> c;
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,c;
f>>x>>y>>c;
gr[x].push_back(make_pair(y,c));
}
for(int i=2;i<=n;i++)
{
cost[i]=INT_MAX;
}
cost[1]=0;
c.push(1);
while(!c.empty())
{
int fr=c.front();
c.pop();
for(auto i:gr[fr])
{
if(cost[fr]+i.second<cost[i.first])
{
act[i.first]++;
cost[i.first]=cost[fr]+i.second;
if(act[i.first]>n)
{
g<<"Ciclu negativ!";
return 0;
}
c.push(i.first);
}
}
}
for(int i=2;i<=n;i++)
g<<cost[i]<<" ";
return 0;
}