Traffic LIGHTS POJ 1158

Source: Internet
Author: User
Tags getcolor

Main topic:

The Dingilville city arrangement is an unusual way of connecting each intersection with a road that connects up to two different intersections. Each intersection cannot connect himself. The time on one end of a road trip is the same, and at any intersection there is a traffic light, which can only be red or green at any time. It is permitted to reach another intersection from one intersection only when the color of the lights at the two intersections is the same. If a car arrives at a junction where the lamp is converted, it must consider the color of the lamp. Allow the vehicle to wait at the intersection.

Here is a map of the city:

The pass time of all roads is an integer, and the traffic light conversion time at each intersection is also an integer and the time of all lights conversion is also an integer. Your mission is to get from the source point to the end point the vehicle you are driving must find a shortest time to arrive, maybe there are more than one you value to output one on the line.

Input data:

2 <= n <=300 is a number of n nodes. Number is from 1->n

1 <=m <=14,000 M is the number of roads

1 <= lij <= 100 distance from I to J

1 <= tic <= 100 for C color duration in node I

1 <= Ric <= tic stands for color C in the remaining time of node I

The first line is two integers, one is the number of the source point, and the other is the number of the end point.

The second line consists of two integers. One is N, M.

Next, n rows represent n intersections.

Each line has CI, Ric, TiB, TiP, CI is ' B ' or ' P ' represents the initial color of the traffic light, Ric represents the color C remaining time, TiP represents the duration of color P, and TiB represents the duration of color B.

Next is the M line representing the distance from node I to node J is L

Output:

If there is a path from the source point to the end point then we output the minimum time, otherwise output 0

Topic Analysis:

The shortest circuit does not say the SPFA water, the difficulty is to judge from one point to another point when you need to judge whether two lights are the same, if the same can go, otherwise not too.

Can not be a time to wait in that, until the time can go again, will wait for the time plus can be counted out.

Count the waiting time when I was violent water, that is, the violence to judge every second whether the same light, if there is, return to this point of time is to pay attention to, if two points of the lamp is the same, this second is also able to go, is wrong in this WA a day, finally wrote a simple data is water passed.

Here is the code + comment

1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <algorithm>5#include <vector>6#include <queue>7#include <cmath>8#include <cstring>9 using namespacestd;Ten #defineINF 0XFFFFFFF One #defineMAXN 520 A struct Point - { -     Charcolor[2];//the color of the current street lamp 1 B 0 P the     intR; -     intP//Color P duration of time -     intB//Color B Duration - } P[MAXN]; +  - structEdge + { A     inte, W; atEdge (intE=0,intw=0): E (E), W (w) {} - }; -Vector<edge>G[MAXN]; -  - BOOLVIS[MAXN]; - intDIST[MAXN]; in intStar, End; - intN, M; to CharGetColor (Point A,intTime//get the color of this traffic light at this point in time + { -     intTimea = time-A.R; the  *     if(Timea <=0) $         returna.color[0];Panax Notoginseng  -Timea = timea% (a.b +A.P); the  +      if(Timea = =0) ATimea = a.b +A.P; the  +     if(a.color[0] =='B') -     { $         if(Timea <=A.P) $             return 'P'; -         Else -             return 'B'; the     } -     ElseWuyi     { the         if(Timea <=a.b) -             return 'B'; Wu         Else -             return 'P'; About     } $  - } - intGetTime (Point A, point B,intTime//get the same time at two traffic lights. - { A     if(a.b = = B.P && A.P = = b.b && a.color[0]! = b.color[0] && A.R = = B.R)//It is impossible to exclude the same situation +         returnINF; the  -      while(1)//time to accumulate, always get the same so far $     { theTime + +;//time to First + 1, because I came from the time is I have spent the time, so to judge him after a second the         if(GetColor (a,time) = =GetColor (b,time)) the             returntime-1;//It is possible to go at this point, but this should not be spent on time, because the time has not yet the     } -  in } the voidSPFA ()//SPFA no longer repeat, the key is to see how to walk between two points the { About Edge Pa, Pn; theQueue<edge>Q; theDist[star] =0; theQ.push (Edge (Star,0) ); +  -      while( !q.empty ()) the     {BayiPa =Q.front (); the Q.pop (); theVIS[PA.E] =false; -         intLen =g[pa.e].size (); -  the          for(intI=0; i<len; i++) the         { thePn =G[pa.e][i]; the  -             intTime = GetTime (P[PA.E], P[PN.E], DIST[PA.E]) + PN.W;//get two lights the same shortest time + distance time Update dist array the  the             if(DIST[PN.E] >Time ) the             {94DIST[PN.E] =Time ; the  the                 if( !VIS[PN.E]) the                 {98VIS[PN.E] =true; About Q.push (Pn); -                 }101             }102         }103     }104 } the voidInit ()106 {107      for(intI=0; i<=n; i++)108     {109 g[i].clear (); theVis[i] =false;111Dist[i] =INF; the     }113 } the  the intMain () the {117 118      while(Cin >> Star >>End)119     { -CIN >> N >>m;121 122 Init ();123 124          for(intI=1; i<=n; i++) the         {126scanf"%s%d%d%d", P[i].color,&p[i]. R,&p[i]. b,&P[i]. P);127         } - 129  the          for(intI=0; I<m; i++)131         { the             intA, B, C;133scanf"%d%d%d",&a,&b,&c);134 135 G[a].push_back (Edge (b,c));136 G[b].push_back (Edge (a,c));137         }138 139 SPFA (); $ 141         if(Dist[end] = =INF)142cout <<0<<Endl;143         Else144cout << Dist[end] <<Endl;145     }146     return 0;147}

Traffic LIGHTS POJ 1158

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.