The Chinese Question is wrong.
Key Point: two people with a level gap greater than m cannot trade directly or indirectly, and 1 must be the end of the graph, therefore, the levels of all vertices in the shortest path must be in the range of [level [1]-M, level [1] + M]. Therefore, you can enumerate the levels,
For (I = level [1]-m; I <= level [1]; I ++)
{
Shortest PathAlgorithmMake sure that
The level of each vertex in the shortest path is between [I, I + M ].
}
In this way, the bloody AC is always so inspiring.
View code
# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
Const Int INF = 1000000000 ;
Int Lev [ 110 ], P [ 110 ];
Int Map [ 110 ] [ 110 ];
Int Vis [ 110 ];
Int Dis [ 110 ];
Int Dijkstra ( Int Cost [] [110 ], Int N, Int X, Int M)
{
Int I, J, K, minc;
Memset (VIS, 0 , Sizeof (VIS ));
Dis [ 0 ] = 0 ; Vis [ 0 ] = 1 ;
For (I = 1 ; I <n; I ++) dis [I] = cost [ 0 ] [I];
For (I = 1 ; I <n; I ++)
{
Minc = inf;
For (J = 0 ; J <n; j ++)
If (! Vis [J] & dis [J] <minc)
{
Minc = dis [J];
K = J;
}
If (Minc = inf) Break ;
Vis [k] = 1 ;
For (J = 0 ; J <n; j ++)
If (! Vis [J] & dis [J]> dis [k] + cost [k] [J] & lev[ K]> = x & lev[ K] <= X + M)
Dis [J] = dis [k] + cost [k] [J];
}
Return Dis [ 1 ];
}
Int Main ()
{
Int M, N;
Int I, J, K, X, price, ID;
While (Scanf ( " % D " , & M, & N )! = EOF)
{
For (I = 0 ; I <= 100 ; I ++)
{
Map [I] [I] =0 ;
For (J = I + 1 ; J <= 100 ; J ++)
{
Map [I] [J] = map [J] [I] = inf;
}
}
For (I = 1 ; I <= N; I ++)
{
Scanf ( " % D " , & P [I], & lev[ I], & X );
For (J = 1 ; J <= x; j ++)
{
Scanf ( " % D " , & ID, & price );
Map [ID] [I] = price;
}
}
For (I = 1 ; I <= N; I ++)
Map [ 0 ] [I] = P [I];
Lev [0 ] = Lev [ 1 ];
Int Low = lev [ 1 ]-M;
Int Ans = inf;
For (I = low; I <= lev [ 1 ]; I ++)
{
Int TMP = Dijkstra (MAP, N + 1 , I, m );
If (TMP <ans)
Ans = TMP;
}
Printf ( " % D \ n " , ANS );
}
Return 0 ;
}