Pagini recente » Cod sursa (job #1231111) | Cod sursa (job #591155) | Cod sursa (job #3261655) | Cod sursa (job #1131157) | Cod sursa (job #3271860)
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
const int NMAX=50005;
const int MMAX=250007;
const int INF=1000000009;
int n,m,a,b,c,i;
int cost[NMAX];
bool change=0;
bool good;
struct ed
{
int x,y,w;
};
ed aux;
vector<ed> edj;
int main()
{
in>>n>>m;
for(i=1;i<=m;i++)
{
in>>a>>b>>c;
aux.x=a;
aux.y=b;
aux.w=c;
edj.push_back(aux);
}
memset(cost,INF,sizeof(cost));
cost[1]=0;
good=0;
for(i=1;i<=n/2;i++)
{
change=0;
for(auto g:edj)
{
if(cost[g.x]+g.w<cost[g.y])
{
change=1;
cost[g.y]=cost[g.x]+g.w;
}
}
if(change==0)
{
good=1;
break;
}
}
if(good==0)
{
out<<"Ciclu negativ!";
}
else
{
for(i=2;i<=n;i++)
{
out<<cost[i]<<" ";
}
}
}