Pagini recente » Cod sursa (job #943847) | Profil Ellie59 | Atasamentele paginii wellcodesimulareclasa11-12-9martie | Cod sursa (job #2737856) | Cod sursa (job #2365744)
#include<fstream>
#include<vector>
#include<queue>
#define pinf 999999999
#define pii pair<int,int>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n,m,x,y,c,i,ok,nod,d[50010],viz[50010];
vector < pii > v[50010];
queue <int> q;
int main()
{
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y>>c;
v[x].push_back({y,c});
}
for(i=1;i<=n;i++)
d[i]=pinf;
d[1]=0;
q.push(1);
while(!q.empty() && ok==0)
{
nod=q.front();
q.pop();
for(auto it : v[nod])
{
if(d[nod] + it.second < d[it.first])
{
d[it.first]=d[nod] + it.second ;
viz[it.first]++;
q.push(it.first);
if(viz[it.first]>n)
ok=1;
}
}
}
if(ok==1)
fout<<"Ciclu negativ!";
else
for(i=2;i<=n;i++)
fout<<d[i]<<' ';
fin.close();
fout.close();
return 0;
}