// BellmanFord.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <vector>
#include <fstream>
#include <tuple>
#include <queue>
using namespace std;
const int MAX_N = 50000 + 1;
const int MAX_M = 250000 + 1;
const int oo = static_cast<int>(1e9);
vector<vector<tuple<int, int>>> adj(MAX_N + 1, vector<tuple<int, int>>());
int N, M;
int s;
vector<int> d(MAX_N);
vector<int> cnt(MAX_N);
bool SPFA()
{
for (int i = 1; i <= N; ++i)
{
d[i] = oo;
cnt[i] = 0;
}
d[s] = 0;
queue<int> Q;
Q.push(s);
cnt[s] = 1;
while (Q.empty() == false)
{
int u = Q.front();
Q.pop();
for (auto& edge : adj[u])
{
int v = get<0>(edge);
int weight = get<1>(edge);
if (d[u] + weight < d[v])
{
d[v] = d[u] + weight;
Q.push(v);
cnt[v] += 1;
if (cnt[v] == N)
{
return false;
}
}
}
}
return true;
}
int main()
{
ifstream fin{ "bellmanford.in" };
ofstream fout{ "bellmanford.out" };
s = 1;
fin >> N >> M;
for (int i = 1; i <= M; ++i)
{
int u, v, weight;
fin >> u >> v >> weight;
adj[u].push_back({ v, weight });
}
bool ok = SPFA();
if (ok == false)
{
fout << "Ciclu negativ!" << endl;
}
else
{
for (int i = 2; i <= N; ++i)
{
fout << d[i] << " ";
}
fout << endl;
}
return 0;
}