Pagini recente » Cod sursa (job #591522) | Cod sursa (job #318065) | Cod sursa (job #538463) | Cod sursa (job #3216011) | Cod sursa (job #3215859)
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
#define pb push_back
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
const int N = 50005;
const int oo = 0x3f3f3f3f;
int n;
vector<int> c, cnt;
vector<vector<pii>> e;
void read(), bellmanford();
int main(){
read();
bellmanford();
for (int i = 2; i <= n; i++)
fout << c[i] << ' ';
return 0;
}
void read(){
int m; fin >> n >> m;
e.resize(n+2); c.resize(n+2, oo); cnt.resize(n+2);
while (m--){
int a, b, k; fin >> a >> b >> k;
e[a].pb({b, k});
}
}
void bellmanford(){
queue<pii> s; s.push({0, 1}); c[1] = 0;
while (!s.empty()){
int cst, from;
tie(cst, from) = s.front();
s.pop();
for (auto it: e[from]){
int to, cm; tie(to, cm) = it;
if (cm + cst < c[to]){
cnt[to]++;
if (cnt[to] == n+2){
fout << "Ciclu negativ!";
exit(0);
}
c[to] = cst + cm;
s.push({c[to], to});
}
}
}
}