Pagini recente » Cod sursa (job #1989866) | Clasament simulare-fminostress2017 | Cod sursa (job #1983979) | Cod sursa (job #195145) | Cod sursa (job #2345971)
#include <bits/stdc++.h>
using namespace std;
#if 1
#define pv(x) std::cerr<<#x<<" = "<<(x)<<"; ";std::cerr.flush()
#define pn std::cerr<<std::endl
#else
#define pv(x)
#define pn
#endif
#ifdef INFOARENA
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
#else
#define in cin
#define out cout
#endif
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pld = pair<ld, ld>;
#define pb push_back
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
const int inf_int = 1e9 + 5;
const ll inf_ll = 1e18 + 5;
const int NMax = 5e4 + 5;
const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0};
const double EPS = 1e-8;
int dist[NMax];
bool inQueue[2][NMax];
vector<pii> adj[NMax];
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int N,M;
in >> N >> M;
for (int i = 1; i <= M; ++i) {
int x,y,c;
in >> x >> y >> c;
adj[x].pb({y, c});
}
for (int i = 2; i <= N; ++i) {
dist[i] = inf_int;
}
dist[1] = 0;
bool update = false;
queue<int> Q[2];
Q[1].push(1);
for (int i = 1; i <= N; ++i) {
update = false;
int here = i % 2, there = here ^ 1;
while (Q[here].size()) {
int node = Q[here].front();
Q[here].pop();
inQueue[here][node] = false;
for (auto& p : adj[node]) {
int nxt = p.first, cost = p.second;
if (dist[nxt] > dist[node] + cost) {
dist[nxt] = dist[node] + cost;
update = true;
if (!inQueue[there][node]) {
Q[there].push(nxt);
inQueue[there][nxt] = true;
}
}
}
}
}
if (update) {
out << "Ciclu negativ!\n";
}
else {
for (int i = 2; i <= N; ++i) {
out << dist[i] << ' ';
}
}
return 0;
}