include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<mutex>
#include<set>
using namespace std;
struct Pathe
{
int start;
int end;
int num;
};
int main()
{
int olderE;
int newE;
//int N=6;
while (cin>> olderE >> newE )
{
int max = 100000;
vector<int> nums;
vector<int>starts;
vector<int> ends;
vector<int> sizes;
int start;
int end;
int si;
while (cin >> start >> end >> si)
{
starts.push_back(start);
ends.push_back(end);
sizes.push_back(si);
nums.push_back(start);
nums.push_back(end);
// cout << nums.size() << endl;
}
sort(nums.begin(), nums.end());
auto pos = unique(nums.begin(), nums.end());
nums.erase(pos,nums.end());
vector<vector<int>> vec(nums.size(), vector<int>(nums.size(), max));
for (int i = 0;i < starts.size();i++)
{
int x = find(nums.begin(),nums.end(),starts[i])-nums.begin() ;
int y = find(nums.begin(), nums.end(), ends[i]) - nums.begin();
vec[x][y] = sizes[i];
vec[y][x] = sizes[i];
}
////输出元素的值
//cout << "得到的矩阵是:"<<vec.size() << endl;
//for (int i = 0;i <vec.size();i++)
//{
// for (int j = 0;j < vec.size();j++)
// {
// if (i == j)
// {
// vec[i][j] = 0;
// }
// cout << vec[i][j] << " ";
// // cout << vec[j][i] << " ";
// }
// cout << endl;
//}
//记录所求节点到其他节点的距离
vector<int> distance(vec.size(),max);
vector<vector<int>> nodesPath;//通过的节点
//记录上一次的路径长度
vector<int> path(vec.size(), max);
//记录已经分析过的节点
vector<int> nodes;
for (int i = 0;i < vec.size();i++)
{
nodes.push_back(i);
for (int j = 0;j < nodes.size();j++)
{
vector<int> np;
for (int k = 0;k < vec.size();k++)
{
if (i == 0)
{
distance[k] = vec[i][k];
np.push_back(k);
nodesPath.push_back(np);
}
if (path[j] + vec[j][k] < distance[k])
{
distance[k] = path[j] + vec[j][k];
nodesPath[k] = nodesPath[j];
nodesPath[k].push_back(k);
}
}
path = distance;
}
}
/*// 输出距离
cout << "v0到各个节点的距离是:" << endl;
for (int i = 0;i < distance.size();i++)
{
cout << distance[i] << " ";
}*/
// cout << endl;
// for (int i = 0;i < nodesPath.size();i++)
for (int i = nodesPath.size()-1;i < nodesPath.size();i++)
{
for (int j = 0;j < nodesPath[i].size()-1;j++)
{
cout << nums[nodesPath[i][j]] << "->";
}
cout<<nums[nodesPath[i][nodesPath[i].size()-1]]<<"("<<distance[i]<<")";
cout << endl;
}
}
return 0;
}