Pagini recente » Cod sursa (job #1227431) | Cod sursa (job #3224466) | Cod sursa (job #301921) | Cod sursa (job #3288580) | Cod sursa (job #2181168)
#include <fstream>
#include <vector>
#define N 50002
#define Inf 1<<30
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n, m, d[N];
vector<pair<int,int>>A[N];
int main()
{
int a,b,c;
fin>>n>>m;
for(int i=m; i; i--)
{
fin>>a>>b>>c;
A[a].push_back({b,c});
}
for(int i=2; i<=n; i++)
d[i]=Inf;
for(int i=1; i<=n; i++)
{
for(auto it:A[i])
{
if(d[i]+it.second<d[it.first])
{
d[it.first]=d[i]+it.second;
}
}
}
for(int i=1; i<=n; i++)
{
for(auto it:A[i])
{
if(d[i]+it.second<d[it.first])
{
fout<<"Ciclu negativ!";
return 0;
}
}
}
for(int i=2; i<=n; i++)
fout<<d[i]<<' ';
return 0;
}