Pagini recente » Cod sursa (job #1028929) | Cod sursa (job #175648) | Cod sursa (job #1552615) | Cod sursa (job #899548) | Cod sursa (job #2646025)
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int t, n, m, k, a[300010], q, l, r;
ifstream fin("bellmanford.in"); ofstream fout("bellmanford.out");
#define cin fin
#define cout fout
pair<pii, int> e[250010];
int d[50010];
int32_t main(){
INIT
cin>>n>>m;
int inf=((int)10e17);
for(int i=2; i<=n; i++){d[i]=inf;}
d[1]=0;
for(int i=1; i<=m; i++){
int x, y, c;cin>>x>>y>>c;
e[i]=mp(mp(x, y), c);
}
for(int i=1; i<n; i++){
for(int j=1; j<=m; j++){
if(d[e[j].ft.ft]!=inf){
d[e[j].ft.sc]=min(d[e[j].ft.sc], d[e[j].ft.ft]+e[j].sc);}
}
}
for(int j=1; j<=m ;j++){
if(d[e[j].ft.sc]>(d[e[j].ft.ft]+e[j].sc) ){
cout<<"Ciclu negativ!"; return 0;
}
}
for(int i=2; i<=n; i++){
cout<<d[i]<<" ";
}
return 0;
}