[Html]
# Include <stdio. h>
# Include <string. h>
# Include <iostream>
# Include <queue>
Using namespace std;
Constint INF = 1000000;
Const int maxn = 10;
Struct ArcNode
{
Int;
Int weight;
ArcNode * next;
};
Queue <int> Q;
Int n;
ArcNode * List [maxn];
Int inq [maxn];
Int dist [maxn], path [maxn];
Void SPFA (int src)
{
Int I, u;
ArcNode * temp;
For (I = 0; I <n; I ++)
{
Dist [I] = INF;
Path [I] = src;
Inq [I] = 0;
}
Dist [src] = 0;
Path [src] = src;
Inq [src] ++;
Q. push (src );
While (! Q. empty ())
{
U = Q. front ();
Q. pop ();
Inq [u] --;
Temp = List [u];
While (temp! = NULL)
{
Int v = temp->;
If (dist [v]> dist [u] + temp-> weight)
{
Dist [v] = dist [u] + temp-> weight;
Path [v] = u;
If (! Inq [v])
{
Q. push (v );
Inq [v] ++;
}
}
Temp = temp-> next;
}
}
}
Int main ()
{
Int I, j;
Int u, v, w;
Scanf ("% d", & n );
Memset (List, 0, sizeof (List ));
ArcNode * temp;
While (1)
{
Scanf ("% d", & u, & v, & w );
If (u =-1 & v =-1 & w =-1)
Break;
Temp = new ArcNode;
Temp-> to = v;
Temp-> weight = w;
Temp-> next = NULL;
If (List [u] = NULL)
List [u] = temp;
Else
{
Temp-> next = List [u];
List [u] = temp;
}
}
SPFA (0 );
For (j = 0; j <n; j ++)
{
Temp = List [j];
While (temp! = NULL)
{
List [j] = temp-> next;
Delete temp;
Temp = List [j];
}
}
Int shortest [maxn];
For (I = 1; I <n; I ++)
{
Printf ("% d \ t", dist [I]);
Memset (shortest, 0, sizeof (shortest ));
Int k = 0;
Shortest [k] = I;
While (path [shortest [k]! = 0)
{
K ++;
Shortest [k] = path [shortest [k-1];
}
K ++;
Shortest [k] = 0;
For (j = k; j> 0; j --)
Printf ("% d->", shortest [j]);
Printf ("% d \ n", shortest [0]);
}
}
# Include <stdio. h>
# Include <string. h>
# Include <iostream>
# Include <queue>
Using namespace std;
Constint INF = 1000000;
Const int maxn = 10;
Struct ArcNode
{
Int;
Int weight;
ArcNode * next;
};
Queue <int> Q;
Int n;
ArcNode * List [maxn];
Int inq [maxn];
Int dist [maxn], path [maxn];
Void SPFA (int src)
{
Int I, u;
ArcNode * temp;
For (I = 0; I <n; I ++)
{
Dist [I] = INF;
Path [I] = src;
Inq [I] = 0;
}
Dist [src] = 0;
Path [src] = src;
Inq [src] ++;
Q. push (src );
While (! Q. empty ())
{
U = Q. front ();
Q. pop ();
Inq [u] --;
Temp = List [u];
While (temp! = NULL)
{
Int v = temp->;
If (dist [v]> dist [u] + temp-> weight)
{
Dist [v] = dist [u] + temp-> weight;
Path [v] = u;
If (! Inq [v])
{
Q. push (v );
Inq [v] ++;
}
}
Temp = temp-> next;
}
}
}
Int main ()
{
Int I, j;
Int u, v, w;
Scanf ("% d", & n );
Memset (List, 0, sizeof (List ));
ArcNode * temp;
While (1)
{
Scanf ("% d", & u, & v, & w );
If (u =-1 & v =-1 & w =-1)
Break;
Temp = new ArcNode;
Temp-> to = v;
Temp-> weight = w;
Temp-> next = NULL;
If (List [u] = NULL)
List [u] = temp;
Else
{
Temp-> next = List [u];
List [u] = temp;
}
}
SPFA (0 );
For (j = 0; j <n; j ++)
{
Temp = List [j];
While (temp! = NULL)
{
List [j] = temp-> next;
Delete temp;
Temp = List [j];
}
}
Int shortest [maxn];
For (I = 1; I <n; I ++)
{
Printf ("% d \ t", dist [I]);
Memset (shortest, 0, sizeof (shortest ));
Int k = 0;
Shortest [k] = I;
While (path [shortest [k]! = 0)
{
K ++;
Shortest [k] = path [shortest [k-1];
}
K ++;
Shortest [k] = 0;
For (j = k; j> 0; j --)
Printf ("% d->", shortest [j]);
Printf ("% d \ n", shortest [0]);
}
} [Html]
/*
/* [Html]
7
0 1 6
0 2 5
0 3 5
1 4-1
2 1-2
2 4 1
3 2-2
3 5-1
4 6 3
5 6 3
-1-1-1
1 0-> 3-> 2-> 1
3 0-> 3-> 2
5 0-> 3
0 0-> 3-> 2-> 1-> 4
4 0-> 3-> 5
3 0-> 3-> 2-> 1-> 4-> 6
7
0 1 6
0 2 5
0 3 5
1 4-1
2 1-2
2 4 1
3 2-2
3 5-1
4 6 3
5 6 3
-1-1-1
1 0-> 3-> 2-> 1
3 0-> 3-> 2
5 0-> 3
0 0-> 3-> 2-> 1-> 4
4 0-> 3-> 5
3 0-> 3-> 2-> 1-> 4-> 6
[Html]
*/
*/