For the second time, given a level limit of M, we can solve the problem by enumerating the position of the first vertex. The idea is clear.
For (INT I = 0; I <= m; ++ I) indicates the offset of the class of the first vertex within the m length range. then you can build an edge in the interval, Floyd.
The Code is as follows:
# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define INF 0x3f3f3fusing namespace STD; int m, n, mey [105], LVL [105], head [105], idx; int lwall, rwall, G [105] [105]; struct edge {int V, Val, next;} e [10005]; // reserved replacement list void addedge (int x, int V, int Val) {++ idx; E [idx]. V = V, E [idx]. val = val; E [idx]. next = head [X], head [x] = idx;} inline bool in (int x) {I F (LVL [x]> = lwall & LVL [x] <= rwall) {return true;} else return false;} void build () {memset (G, 0x3f, sizeof (g); For (INT I = 1; I <= N; ++ I) {G [I] [I] = 0; for (Int J = head [I]; J! =-1; j = E [J]. next) {If (in (I) & in (E [J]. v) {G [I] [E [J]. v] = E [J]. val ;}}} void Floyd (Int & RET) {for (int K = 1; k <= N; ++ K) {for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) {If (G [I] [k]! =-1 & G [k] [J]! =-1) {// G [I] [J] = min (G [I] [J], G [I] [k] + G [k] [J]) ;}}}for (INT I = 1; I <= N; ++ I) {If (G [1] [I]! = Inf) {ret = min (Ret, G [1] [I] + mey [I]) ;}} int main () {int C, V, Val, RET; while (scanf ("% d", & M, & n) = 2) {ret = inf; idx =-1; memset (Head, 0xff, sizeof (head); For (INT I = 1; I <= N; ++ I) {scanf ("% d", & mey [I], & LVL [I], & C); For (Int J = 1; j <= C; ++ J) {scanf ("% d", & V, & Val); addedge (I, V, Val) ;}// enumerate the leftmost and rightmost for (INT I = 0; I <= m; ++ I) {lwall = LVL [1]-I, rwall = lwall + m; If (lwall <0) break; // The left boundary cannot be a negative number. // confirm that the left and right boundary is building (); Floyd (RET);} printf ("% d \ n", RET );} return 0 ;}