Question:
Here We Go (relians) Again
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 360 Accepted Submission (s): 204
Problem Description
The Gorelians are 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 all Gorelian Government Officials to the Government Building by the fastest possible route.
Fortunately for the Gorelian Minister of Traffic (that wocould be you), all Gorelian cities are laid out as a rectangular grid of blocks, where each block is a square measuring 2520 rels per side (a rel is the Gorelian Official Unit of Distance ). the speed limit between two adjacent intersections is always constant, and may range from 1 to 9 rels per blip (a blip, of course, being the Gorelian Official Unit of Time ). since Gorelians have outlawed decimal numbers as unholy (hey, if you're the dominant force in the known universe, you can outlaw whatever you want), speed limits are always integer values. this explains why Gorelian blocks are precisely 2520 rels in length: 2520 is the least common multiple of the integers 1 through 9. thus, the time required to travel between two adjacent intersections is always an integer number of blips.
In all Gorelian cities, Government Housing is always at the northwest corner of the city, while the Government Building is 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 traffic patterns causes a lot of accidents ). your job, given the details of speed limits, street directions ctions, and street closures for a Gorelian city, is to determine the fastest route from Government Housing to the Government Building. (It is possible, due to street directions and closures, that 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 way streets, and one closed street. it is assumed that streets are always traveled at the exact posted speed limit, and that turning a corner takes zero time. under these conditions, you shocould be able to determine that the fastest route from Government Housing to the Government Building in this city is 1715 blips. and if the next day, the only change is that the closed road is opened to two way traffic at 9 rels per blip, the fastest route becomes 1295 blips. on the other hand, suppose the three one-way streets are switched from southbound to northbound (with the closed road remaining closed ). in that case, no route wocould be possible and the day wocould be declared a holiday.
Input
The input consists of a set of cities for which you must find a fastest route if one exists. the first line of an input case contains two integers, which are the vertical and horizontal number of city blocks, respectively. the smallest city is a single block, or 1 by 1, and the largest city is 20 by 20 blocks. the remainder of the input specifies speed limits and traffic directions ctions for streets between intersections, one row of street 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 streets. speed limits and directions of travel are specified in order from west to east, and each consists of an 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 are delimited by a single space. for east-west streets, the symbol will be an asterisk '*' which indicates travel is allowed in both directions ctions, a less-than symbol' <'which indicates travel is allowed only in an east-to-west ction, or a greater-than symbol'> 'which indicates travel is allowed only in a west-to-east ction. for north-south streets, an asterisk again indicates travel is allowed in either ction, a lowercase "vee" character 'V' indicates travel is allowed only in a north-to-south ctions, and a caret symbol' ^ 'indicates travel is allowed only in a south-to-north ction. A zero speed, indicating a closed road, is always followed by an asterisk. input cities continue in this manner until a value of zero is specified for both the vertical and horizontal dimensions.
Output
For each input scenario, output a line specifying the integer number of blips of the shortest route, a space, and then the word "blips ". for scenarios which have no route, output a line with the word "Holiday ".
Sample Input
2 2
9*9 *
6 v 0*8 v
3*7 *
3*6 v 3 *
4*8 *
2 2
9*9 *
6 v 9*8 v
3*7 *
3*6 v 3 *
4*8 *
2 2
9*9 *
6 ^ 0*8 ^
3*7 *
3*6 ^ 3 *
4*8 *
0 0
Sample Output
1715 blips
1295 blips
Holiday
Question:
The question is a little disgusting. N * m-sized rectangle, with the starting point in the upper left corner of the rectangle and the ending point in the lower right corner. A small rectangle in it represents a block ). The side length of each small rectangle is 2520. The side of the small rectangle has a speed limit, ranging from 0 ~ 9. If the value is 0, this edge cannot be traveled.
The input part is indeed disgusting. From top to bottom, from left to right, the data is given based on the corresponding position, each edge is in the form of "Number" + "space" + "symbol". A number indicates the speed limit of this edge. A symbol indicates that this path is unidirectional (also divided between East and West, North and South) or two-way.
Analysis and Summary:
The key to this question is to read the question and process the input. After creating the image, you can use any of the shortest path algorithms.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <utility>
Using namespace std;
Typedef pair <int, int> pii;
Const int INF = 0x7fffffff;
Const int VN = 445;
Const int EN = VN * VN/2;
Struct Edge {
Int v, next, w;
} E [EN];
Int n;
Int m;
Int vn;
Int size;
Int head [VN];
Int d [VN];
Void addEdge (int u, int v, int w ){
E [size]. v = v;
E [size]. w = w;
E [size]. next = head [u];
Head [u] = size ++;
}
Void init (){
Vn = (m + 1) * (n + 1 );
Size = 0;
Memset (head,-1, sizeof (head ));
}
Void Dijkstra (int src ){
For (int I = 1; I <= vn; ++ I) d [I] = INF;
D [src] = 0;
Priority_queue <pii, vector <pii>, greater <pii> q;
Q. push (make_pair (d [src], src ));
While (! Q. empty ()){
Pii x = q. top (); q. pop ();
Int u = x. second;
If (d [u]! = X. first) continue;
For (int e = head [u]; e! =-1; e = E [e]. next ){
Int tmp = d [u] + E [e]. w;
If (d [E [e]. v]> tmp ){
D [E [e]. v] = tmp;
Q. push (make_pair (tmp, E [e]. v ));
}
}
}
}
Int main (){
Char str [100];
Int u, v, w;
While (~ Scanf ("% d % * c", & n, & m) & n + m ){
// Input
Init ();
For (int I = 1; I <= n * 2 + 1; ++ I ){
Gets (str );
Int len = strlen (str );
If (I & 1 ){
For (int j = 0, k = 1; j <len; j + = 4, ++ k ){
U = (m + 1) * (I/2) + k;
W = str [j]-'0 ';
If (w = 0) continue;
If (str [j + 2] = '*'){
AddEdge (u, u + 1,2520/w );
AddEdge (u plus 1, u, 2520/w );
}
Else if (str [j + 2] = '<'){
AddEdge (u plus 1, u, 2520/w );
}
Else {
AddEdge (u, u + 1,2520/w );
}
}
}
Else {
For (int j = 0, k = 1; j <len; j + = 4, ++ k ){
U = (m + 1) * (I/2-1) + k;
W = str [j]-'0 ';
If (w = 0) continue;
If (str [j + 2] = '*'){
AddEdge (u, u + m + 1, 2520/w );
AddEdge (u + m + 1, u, 2520/w );
}
Else if (str [j + 2] = 'V '){
AddEdge (u, u + m + 1, 2520/w );
}
Else if (str [j + 2] = '^ '){
AddEdge (u + m + 1, u, 2520/w );
}
}
}
}
Dijkstra (1 );
If (d [vn]! = INF) printf ("% d blips \ n", d [vn]);
Else puts ("Holiday ");
}
Return 0;
}