Poj1158-traffic lights

Source: Internet
Author: User

View more problem solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
reprinted please indicate the source: http://blog.csdn.net/wangjian8006 >

 

In a graph, there are n vertices, some vertices have paths, and each vertex has a color, 'B' and 'P'. When the two vertices have the same color, and there is a path to go, each point in each color has a stay cycle, after the cycle will change to the next color
Returns S and T, representing the shortest time from S to T.
Then there are n knots and m edges. First, the n lines represent the initial color of each node, and the color will change after Ric [I, the cycle time when the color is 'B' and the cycle time when the color is 'P. Next, m edges represent the undirected edges. The edge weight is the time used to pass through the edge.

Solution: a shortest path for deformation, but it should be noted that the formula is that t time has passed, and at least wait time is required for access from node v to node u. You just need to find out the waiting time and change the spfa.

/* Spfamemory 540 ktime 157 ms */# include <iostream> # include <queue> using namespace STD; # define maxv 310 # define INF 1 <27 # define min (, b) (A> B? B: A) int s, t, n, m, map [maxv] [maxv]; int Ric [maxv], TB [maxv], TP [maxv]; char C [maxv]; // here, the status 0 is B, and 1 is P // view the color of the node after now_time, and the int status (INT now_time, int node, Int & R) {If (C [node] = 'B') will change again after the r time ') {If (now_time-ric [node] <0) {r = Ric [node]-now_time; return 0;} now_time = (now_time-ric [node]) % (TP [node] + Tb [node]); If (now_time <TP [node]) {r = TP [node]-now_time; return 1 ;} R = Tb [node]-now_time + TP [node]; return 0;} else {If (now_t IME-ric [node] <0) {r = Ric [node]-now_time; return 1;} now_time = (now_time-ric [node]) % (TP [node] + Tb [node]); If (now_time <TB [node]) {R = Tb [node]-now_time; return 0 ;} R = TP [node]-now_time + Tb [node]; return 1 ;}/// obtain the ini_time time, minimum wait time from start to end point int Ti (INT ini_time, int start, int end) {int C1, C2, R1, R2; C1 = status (ini_time, start, r1); // wait time. The first step is to check whether the statuses of the two nodes after ini_time are consistent C2 = status (ini_time, end, R2); If (C1 = c2) return 0; // consistent status You do not need to wait // If the status is different, check whether the time for the two nodes to change to the next state after ini_time is the same if (R1! = R2) return min (R1, R2); // if the return time is different, it must be changed to the next time, because they are in the same status. If (C1 = 0) {// if the time is the same, then we can see the next state time if (TP [start] <TB [end]) return R1 + TP [start]; If (TP [start]> TB [end]) return r2 + Tb [end]; Return-1;} else {If (Tb [start] <TP [end]) return R1 + Tb [start]; if (Tb [start]> TP [end]) return r2 + TP [end]; Return-1 ;}} void spfa () {queue <int> q; int V, i, d [maxv], TMP; bool vis [maxv]; memset (VIS, false, sizeof (VIS); for (I = 0; I <= N; I ++) d [I] = INF; d [s] = 0; vis [s] = true; q. Push (s); While (! Q. empty () {v = Q. front (); q. pop (); vis [v] = false; for (I = 1; I <= N; I ++) {TMP = Ti (d [v], V, i); // calculate the wait time for this path. If it is-1, it will never pass through if (Map [v] [I] & TMP! =-1 & D [I]> d [v] + map [v] [I] + TMP) {d [I] = d [v] + map [v] [I] + TMP; If (! Vis [I]) {q. push (I); vis [I] = true ;}}} if (d [T]> = inf) printf ("0 \ n "); else printf ("% d \ n", d [T]);} int main () {int I; int A, B, TMP; while (~ Scanf ("% d", & S, & T) {scanf ("% d", & N, & M); getchar (); for (I = 1; I <= N; I ++) scanf ("% C % d \ n", & C [I], & Ric [I], & TB [I], & TP [I]); // record the initial state of the node and how long it will take to change color, the time when the color is 'B', and the time when the color is 'P' for (I = 1; I <= m; I ++) {// enter M undirected edges scanf ("% d", & A, & B, & TMP ); map [B] [a] = map [a] [B] = TMP;} spfa (); // calculate the Shortest Path} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.