/*
Question:
Find the minimum ring and output path in the graph.
Analysis:
You can use enumeration to delete an edge to see if it can be moved from one point to another.
If the length of the deleted edge is smaller than the current optimal solution, update the path and optimal solution. The print path can be represented by an array.
Run the previous vertex and print the path recursively. The shortest path can be Dijkstra.Algorithm. The estimated time complexity is O (n ^ 2 * E ).
I handed it in. memory usage: 1.328: 264 KB
*/
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace STD;
Const int x = 105;
Constint INF = 100000000;
Int map [x] [X], pre [X], ANS [X], DIS [X], n, m;
Bool use [x];
Int Dijkstra (int s, int e) {// start point S, end point E
Memset (use, false, sizeof (use ));
Memset (PRE, 0, sizeof (pre ));
For (INT I = 1; I <= N; I ++)
Dis [I] = inf;
Int min, K;
Dis [s] = 0;
For (INT I = 1; I <= N; I ++ ){
Min = inf;
For (Int J = 1; j <= N; j ++)
If (! Use [J] & dis [J] <min)
Min = dis [k = J];
If (min = inf)
Return INF;
Use [k] = true;
For (Int J = 1; j <= N; j ++)
If (! Use [J] & dis [J]> dis [k] + map [k] [J]) {
Dis [J] = dis [k] + map [k] [J];
Pre [J] = K; // The first vertex of J is K.
}
}
Return dis [E];
}
Void print (int pos) {// recursive print path Function
If (! Ans [POS])
Return;
Print (ANS [POS]);
Printf ("% d", ANS [POS]);
}
Int main (){
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int x, y, z;
While (CIN> n, n! =-1 ){
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= N; j ++)
Map [I] [J] = inf; // The input sample data of the question contains backward edge input and is an undirected graph.
Cin> m;
For (INT I = 1; I <= m; I ++ ){
Scanf ("% d", & X, & Y, & Z );
If (Map [x] [Y]> Z) // if it is less than the preceding length, update
Map [x] [Y] = map [y] [x] = z;
}
Int temp, RET, CNT = inf, Pos =-1; // enumerate all edges
For (Int J = 1; j <= N; j ++ ){
For (INT I = 1; I <= N; I ++ ){
If (Map [J] [I] <inf ){
Temp = map [J] [I];
Map [J] [I] = map [I] [J] = inf;
Ret = Dijkstra (J, I) + temp;
Map [J] [I] = map [I] [J] = temp;
If (Ret <CNT ){
Pos = I;
For (int K = 1; k <= N; k ++)
Ans [k] = pre [k];
CNT = ret;
}
}
}
}
If (Pos =-1) // when Pos =-1, it indicates that the graph has no loops.
Printf ("no solution. \ n ");
Else {
Print (POS );
Cout <POS <Endl;
}
}
Return 0;
}