#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct WarshallFloyd { std::vector> graph, dist; WarshallFloyd(const std::vector> &graph, const T TINF) : graph(graph), dist(graph), TINF(TINF) { n = graph.size(); internal.assign(n, std::vector(n, -1)); for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { if (dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; internal[i][j] = k; } } } void add(int src, int dst, T cost) { srcs.emplace_back(src); dsts.emplace_back(dst); costs.emplace_back(cost); } void calc() { std::set vers; int sz = srcs.size(); for (int i = 0; i < sz; ++i) { int s = srcs[i], t = dsts[i]; T cost = costs[i]; if (cost < graph[s][t]) graph[s][t] = cost; if (dist[s][t] >= cost) { dist[s][t] = cost; internal[s][t] = -1; } vers.emplace(s); vers.emplace(t); } for (int v : vers) { for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { if (dist[i][j] > dist[i][v] + dist[v][j]) { dist[i][j] = dist[i][v] + dist[v][j]; internal[i][j] = v; } } } srcs.clear(); dsts.clear(); costs.clear(); } bool has_negative_cycle() const { for (int i = 0; i < n; ++i) { if (dist[i][i] < 0) return true; } return false; } std::vector build_path(int s, int t) const { std::vector res; if (dist[s][t] != TINF) { build_path(s, t, res); res.emplace_back(t); } return res; } private: const T TINF; int n; std::vector> internal; std::vector srcs, dsts; std::vector costs; void build_path(int s, int t, std::vector &path) const { int k = internal[s][t]; if (k == -1) { path.emplace_back(s); } else { build_path(s, k, path); build_path(k, t, path); } } }; int main() { int n, m; cin >> n >> m; vector g(n, vector(n, LINF)); REP(i, n) g[i][i] = 0; while (m--) { int s, t; ll d; cin >> s >> t >> d; --s; --t; chmin(g[s][t], d); } WarshallFloyd wf(g, LINF); REP(i, n) { ll ans = 0; REP(j, n) { if (wf.dist[i][j] != LINF) ans += wf.dist[i][j]; } cout << ans << '\n'; } return 0; }