Spfa.
Although it is very troublesome, it is actually a short circuit with a restriction.
Here are some points, edges, start points, and end points.
Then it takes time to pass through these edges, but there are limits on the switch.
Ask you the shortest path to reach the key. (Undirected graph)
For example, input:
1 2 6 2 10 22 30
1-> 2 takes 6 times.
0 ~ 1, 1 ~ 2, 2 ~ 3, 3 ~ 4, 4 ~ 5
0 ~ 2 open, 2 ~ 10 close, 10 ~ 22 open, 22 ~ Close 30, 33 ~ INF opens.
When I save the switch status, I save one more 0 and one INF. Easy to judge.
If the number is less than the odd number, it is enabled, and the even number is disabled.
# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <queue> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6using namespace STD; int n, m; struct lx {int V, W; vector <int> open ;}; vector <lx> G [51]; void spfa (INT start, int thend) {int dis [51]; bool vis [51]; for (INT I = 1; I <= N; I ++) dis [I] = inf, Vis [I] = 0; queue <int> q; DIS [start] = 0, vis [start] = 1; q. Push (start); While (! Q. empty () {int u = Q. front (); q. pop (); vis [u] = 0; For (Int J = 0; j <G [u]. size (); j ++) {int v = G [u] [J]. v; int T = G [u] [J]. w; int COT = G [u] [J]. open. size (); bool openflag = 1; int K; For (k = 1; k <cot; k ++) {If (K & 1) openflag = 1; else openflag = 0; If (openflag & dis [u] + T <= G [u] [J]. open [k] & T + G [u] [J]. open [k-1] <= G [u] [J]. open [k]) {int wait = G [u] [J]. open [k-1]; int cost = max (wait, DIS [u]); If (openflag & dis [v]> Cost + T) {// PRI Ntf ("% d = % d \ n", U, V, cost + T); print path update dis [v] = Cost + T; If (! Vis [v]) {vis [v] = 1; q. push (v) ;}} break ;}}} if (DIS [thend] = inf) puts ("*"); else printf ("% d \ n ", dis [thend]);} int main () {While (scanf ("% d", & N), n) {int start, thend; scanf ("% d", & M, & START, & thend); For (INT I = 0; I <51; I ++) G [I]. clear (); int U, V, T; char STR [1001]; int numtemp [1001]; getchar (); While (M --) {gets (STR ); int cc = 0, numcot = 0; For (INT I = 0; I <= strlen (STR); I ++) {If (STR [I]> = '0' & STR [I] <= '9') CC = Cc * 10 + STR [I]-'0 '; else numtemp [numcot ++] = cc, Cc = 0;} u = numtemp [0], V = numtemp [1], t = numtemp [2]; lx now; now. W = T; now. open. push_back (0); For (INT I = 3; I <numcot; I ++) now. open. push_back (numtemp [I]); now. open. push_back (INF); now. V = V; G [u]. push_back (now); now. V = u; G [v]. push_back (now);}/* For (INT I = 1; I <= N; I ++) {for (Int J = 0; j <G [I]. size (); j ++) {printf ("% d = % d:", I, G [I] [J]. v, G [I] [J]. w); For (int K = 0; k <G [I] [J]. open. size (); k ++) printf ("% d |", G [I] [J]. open [k]); printf ("\ n");} printf ("\ n");} * // storage method spfa (start, thend );}}