[Html]
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
Using namespace std;
Constint INF = 1000000;
Const int maxn = 8;
Int n;
Int edge [maxn] [maxn];
Int dist [maxn];
Int path [maxn];
Void bellman (int v0)
{
Int I, j, k, u;
For (I = 0; I <n; I ++)
{
Dist [I] = edge [v0] [I];
If (I! = V0 & dist [I] <INF)
Path [I] = v0;
Else
Path [I] =-1;
}
For (k = 2; k <n; k ++)
{
For (u = 0; u <n; u ++)
{
If (u! = V0)
{
For (j = 0; j <n; j ++)
{
If (edge [j] [u] <INF & dist [j] + edge [j] [u] <dist [u])
{
Dist [u] = dist [j] + edge [j] [u];
Path [u] = j;
}
}
}
}
}
}
Int main ()
{
Int I, j;
Int u, v, w;
Scanf ("% d", & n );
While (1)
{
Scanf ("% d", & u, & v, & w );
If (u =-1 & v =-1 & w =-1)
Break;
Edge [u] [v] = w;
}
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (I = j)
Edge [I] [j] = 0;
Else
If (edge [I] [j] = 0)
Edge [I] [j] = INF;
}
}
Bellman (0 );
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]);
}
Return 0;
}
<SPAN style = "FONT-FAMILY: Arial, Helvetica, sans-serif"> 7 </SPAN>
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
Using namespace std;
Constint INF = 1000000;
Const int maxn = 8;
Int n;
Int edge [maxn] [maxn];
Int dist [maxn];
Int path [maxn];
Void bellman (int v0)
{
Int I, j, k, u;
For (I = 0; I <n; I ++)
{
Dist [I] = edge [v0] [I];
If (I! = V0 & dist [I] <INF)
Path [I] = v0;
Else
Path [I] =-1;
}
For (k = 2; k <n; k ++)
{
For (u = 0; u <n; u ++)
{
If (u! = V0)
{
For (j = 0; j <n; j ++)
{
If (edge [j] [u] <INF & dist [j] + edge [j] [u] <dist [u])
{
Dist [u] = dist [j] + edge [j] [u];
Path [u] = j;
}
}
}
}
}
}
Int main ()
{
Int I, j;
Int u, v, w;
Scanf ("% d", & n );
While (1)
{
Scanf ("% d", & u, & v, & w );
If (u =-1 & v =-1 & w =-1)
Break;
Edge [u] [v] = w;
}
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (I = j)
Edge [I] [j] = 0;
Else
If (edge [I] [j] = 0)
Edge [I] [j] = INF;
}
}
Bellman (0 );
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]);
}
Return 0;
}
7 [html]
<SPAN style = "FONT-FAMILY: Arial, Helvetica, sans-serif"> 0 1 6 </SPAN>
0 1 6 [html]
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
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