Calculate the shortest path of each vertex, and then calculate the DFS count based on the shortest path of each vertex.
View code
# Include <iostream>
# Include <algorithm>
Using Namespace STD;
Const Int N = 1001 ;
Int G [N] [N], n, m, DIST [N];
Int ANS, DP [N];
Bool Vis [N];
Void Init ()
{
For ( Int I = 0 ; I <= N; I ++)
For ( Int J = 0 ; J <= N; j ++)
G [I] [J] = int_max;
}
Void Dijkstra ( Int S)
{
Memset (VIS, False ,Sizeof (VIS ));
Vis [s] = True ;
For ( Int I = 1 ; I <= N; I ++)
Dist [I] = G [s] [I];
Dist [s] = 0 ;
For ( Int I = 1 ; I <n; I ++)
{
Int Mind = int_max, V =0 ;
For ( Int J = 1 ; J <= N; j ++)
{
If (! Vis [J] & Dist [J] <mind)
{
Mind = DIST [J];
V = J;
}
}
Vis [v] = True ;
For ( Int K = 1 ; K <= N; k ++)
{
If (G [v] [k]! = Int_max &&! Vis [k])
{
Int Newdis = DIST [v] + G [v] [k];
If (Newdis <Dist [k])
Dist [k] = newdis;
}
}
}
}
Int DFS ( Int U)
{
If (U = 2 )
{
Return DP [u] = 1 ;
}
If (DP [u]! =- 1 )
Return DP [u];
Int Sum = 0 ;
For ( Int I =1 ; I <= N; I ++)
{
If (G [u] [I]! = Int_max & Dist [u]> Dist [I])
Sum + = DFS (I );
}
Return DP [u] = sum;
}
Int Main ()
{
Int A, B, C;
While (Scanf ( " % D " , & N) =1 & N)
{
Init ();
Scanf ( " % D " , & M );
For ( Int I = 0 ; I <m; I ++)
{
Scanf ( " % D " , & A, & B, & C );
G [a] [B] = G [B] [a] = C;
}
Dijkstra ( 2 );
Memset (DP ,- 1 , Sizeof (DP ));
Printf ( " % D \ n " , DFS ( 1 ));
}
Return 0 ;
}