Pagini recente » Monitorul de evaluare | Cod sursa (job #2278346) | Cod sursa (job #189932) | Cod sursa (job #3343519) | Cod sursa (job #3341653)
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#define ull unsigned long long
#define mod 1000000007
#define int long long
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
int n,m;
vector<pair<int,int>> adj[50001];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
f>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,z;
f>>x>>y>>z;
adj[x].push_back(make_pair(y,z));
}
vector<int> d(n+1,4e18);
vector<int> cnt(n+1,0);
vector<int> inq(n+1,0);
queue<int> q;
q.push(1);
d[1]=0;
inq[1]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=0;
for(auto x:adj[u])
{
int v=x.first;
int wt=x.second;
if(d[v]>d[u]+wt)
{
d[v]=d[u]+wt;
if(!inq[v])
{
q.push(v);
inq[v]=1;
cnt[v]++;
if(cnt[v]>=n)
{
g<<"Ciclu negativ!";
return 0;
}
}
}
}
}
for(int i=2;i<=n;i++)
g<<d[i]<<" ";
return 0;
}