HDU 2722 here We Go (Relians) Again (SPFA)

Source: Internet
Author: User

Here We Go (Relians) againtime limit:2000/1000ms (java/other) Memory limit:32768/32768k (Java/other) Total Submission ( s): 1 Accepted Submission (s): 1font:times New Roman | Verdana | Georgiafont Size:←→problem DescriptionThe Gorelians is a warlike race that travel the universe conquering new worlds as A form of recreation. Given their violent, fun-loving nature, keeping their leaders alive is of serious concern. Part of the Gorelian security plan involves changing the traffic patterns of their cities on a daily basis, and routing Al L Gorelian Government officials to the government Building by the fastest possible route.

Fortunately for the Gorelian Minister of Traffic (that would is), all Gorelian cities is laid out as a rectangular gr ID of blocks, where each block was a square measuring 2520 rels per side (a rel is the Gorelian official Unit of Distance). The speed limit between-adjacent intersections is all constant, and may range from 1 to 9 rels per blip (a blip, O F course, being the Gorelian official Unit of Time). Since Gorelians has outlawed decimal numbers as unholy (hey, if you ' re the dominant force in the known universe, can Outlaw whatever you want), the speed limits is always the integer values. This explains why Gorelian blocks be precisely 2520 rels in length:2520 is the least common multiple of the integers 1 t Hrough 9. Thus, the time required to travel between and adjacent intersections are always an integer number of blips.

In all Gorelian cities, government Housing are always on the northwest corner of the city and while the government Building are Always at the southeast corner. Streets between intersections might be one-way or two-way, or possibly even closed for repair (all this tinkering with TRA Ffic patterns causes a lot of accidents). Your job, given the details of speed limits, street directions, and street closures for a Gorelian city, was to determine t He fastest route from government Housing to the government Building. (It's possible, due to street directions and closures, that's no route exists, in which case a Gorelian official temporary Holiday is declared, and the Gorelian officials take the day off.)

The picture above shows a Gorelian city marked with speed limits, one to streets, and one closed street. It is assumed that streets be always traveled at the exact posted speed limit, and that turning a corner takes zero time. Under these conditions, you should is able to determine that the fastest route from government Housing to the government Building in 1715 blips. And if the next day, the-the-the-closed road is opened-to-the-traffic at 9 rels per blip, the fastest Route becomes 1295 blips. On the other hand, suppose the three one-way streets is switched from southbound to northbound (with the closed road Rema Ining closed). In this case, no route would is possible and the day would be declared a holiday. Inputthe input consists of a set of cities for which you must find a fastest route if one exists. The first line of a input case contains integers, which is the vertical and horizontal number of city blocks, Respec Tively. The smallest city IS a single block, or 1 by 1, and the largest city is a blocks. The remainder of the input specifies speed limits and traffic directions for streets between intersections, one row of str Segments at a time. The first line of the input, after the dimensions line, contains the data for the northernmost East-west street segments. The next line contains the data for the northernmost row of North-south street segments. Then the next row of East-west streets, then North-south streets, and so on, until the southernmost row of East-west Stree Ts. Speed limits and directions of travel is specified in order from west to east, and each consists of a integer from 0 to 9 indicating speed limit, and a symbol indicating which direction traffic may flow. A Zero speed limit means the road is closed. All digits and symbols is delimited by a single space. For East-west streets, the symbol would be a asterisk ' * ' which indicates travel was allowed in both directions, a Less-tha N symbol ' < ' WHIch indicates travel was allowed only in an east-to-west direction, or a Greater-than symbol ' > ' which indicates travel is allowed only in a west-to-east direction. For North-south streets, the asterisk again indicates travel are allowed in either direction, a lowercase "vee" character ' V ' Indicates travel was allowed only in a north-to-south directions, and a caret symbol ' ^ ' indicates travel was allowed only In a south-to-north direction. A zero speed, indicating a closed road, was always followed by an asterisk. Input cities continue in this manner until a value of zero are specified for both the vertical and horizontal dimensions. Outputfor Each input scenario, output a line specifying the integer number of blips of the shortest route, a space, and th En the word "blips". For scenarios which has no route, output a line with the word "Holiday". Sample Input
2 * 9 *6 v 0 * 8 V3 * 7 * * 6 V 3 * * 8 * * 9 *6 v 9 * 8 V3 * 7 * * 6 V 3 * 8 * * 29 * 9 *6 ^ 0 * 8 ^3 * 7 * 6 ^ 3 * 8 *0 0
Sample Output
1715 blips1295 Blipsholiday

/*This topic is very long, the key is the input, a single input ' * ': means two-way ' > ': Indicates only left-to-right ' < ': Indicates only right-to-left ' ^ ': indicates only from bottom to top ' V ': Indicates that only from the top down if the specified speed on a road is 0, indicating that the output is "Holiday" if not from top left to right, otherwise the shortest time*/#include<iostream>#include<cstdio>#include<vector>#include<queue>using namespacestd;Const intinf=0x7fffffff;structnode{intnum,v; Node (intAintb) {num=a; v=b;}}; Vector<node> s[ A* A];intn,m;BOOLvis[ A* A];intdis[ A* A];voidSPFA () { for(intI=1; I<= (n+1) * (m+1); i++) dis[i]=inf,vis[i]=0; Queue<int>Q; dis[1]=0; vis[1]=1; Q.push (1);  while(!Q.empty ()) {        intu=Q.front ();        Q.pop (); Vis[u]=0;  for(intI=0; I<s[u].size (); i++)        {            if(dis[s[u][i].num]<=dis[u]+2520/S[U][I].V)Continue; Dis[s[u][i].num]=dis[u]+2520/s[u][i].v; if(!Vis[s[u][i].num]) {Vis[s[u][i].num]=1;            Q.push (S[u][i].num); }        }    }}voidRead () {intx; Charch[2];  for(intI=1; I<= (n+1) * (m+1); i++) s[i].clear ();  for(intI=1; i<=m;i++)//first line is processed first{scanf ("%d",&x); scanf ("%s",&ch); if(x==0)Continue; if(ch[0]=='*') {S[i].push_back (node (i+1, x)); S[i+1].push_back (Node (i,x)); }                if(ch[0]=='>') S[i].push_back (Node (i+1, x)); if(ch[0]=='<') s[i+1].push_back (Node (i,x)); }         for(intI=1; i<=n;i++)        {             for(intj=1; J<= (m+1); J + +) {scanf ("%d",&x); scanf ("%s",&ch); if(x==0)Continue; intp= (I-1) * (m+1)+J; intq=i* (m+1)+J; if(ch[0]=='*') {S[p].push_back (node (q,x));                S[q].push_back (Node (p,x)); }                if(ch[0]=='^') S[q].push_back (node (p,x)); if(ch[0]=='v') S[p].push_back (node (q,x)); }             for(intj=1; j<=m;j++) {scanf ("%d",&x); scanf ("%s",&ch); if(x==0)Continue; intp=i* (m+1)+J; intq=i* (m+1) +j+1; if(ch[0]=='*') {S[p].push_back (node (q,x));                S[q].push_back (Node (p,x)); }                if(ch[0]=='<') S[q].push_back (node (p,x)); if(ch[0]=='>') S[p].push_back (node (q,x)); }        }    return;}intMain () { while(SCANF ("%d%d",&n,&m)) {if(n==0&& m==0) Break;        Read ();        SPFA (); if(Dis[(n+1) * (m+1)]==inf) printf ("holiday\n"); Elseprintf"%d blips\n", dis[(n+1) * (m+1)]); }    return 0;}

HDU 2722 here We Go (Relians) Again (SPFA)

Related Article

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.