#include<iostream>
#include<vector>
using namespace std;
vector<int> dijkstra(vector<vector<int>> edge)
{
int n = edge.size();
vector<int> dist(n, 0);
vector<int> flag(n, 0);
flag[0] = 1;
for (int i = 0;i < n;++i)
dist[i] = edge[0][i];
int last_node = 0;
int next_node = last_node;
for (int i = 0; i < n - 1;++i)
{
int mMin = INT_MAX;
for (int j = 0;j < n;++j)
{
if (flag[j] == 0 && dist[j] < mMin)
{
mMin = dist[j];
next_node = j;
}
}
flag[next_node] = 1;
if (next_node == last_node)
continue;
else
{
last_node = next_node;
}
for (int e = 0;e < n;++e)
{
if (edge[next_node][e] < INT_MAX)
{
if (dist[e] > dist[next_node] + edge[next_node][e])
dist[e] = dist[next_node] + edge[next_node][e];
}
}
for (int i = 0;i < n;++i)
cout << dist[i] << " ";
cout << endl;
}
cout << "****************************************************************" << endl;
for (int i = 0;i < n;++i)
cout << dist[i] << " ";
cout << endl;
return dist;
}
int main()
{
int n;
cin >> n;
vector<vector<int>> edge(n, vector<int>(n, 0));
for (int i = 0;i < n;++i)
for (int j = 0;j < n;++j)
if (i == j)
edge[i][j] = 0;
else
edge[i][j] = INT_MAX;
int m, x, y, z;
cin >> m;
for (int i = 0;i < m;++i)
{
cin >> x >> y >> z;
edge[x][y] = z;
}
vector<int> dist = dijkstra(edge);
system("pause");
return 0;
}