POJ 3653 & ZOJ 2935 & HDU 2722 here We Go (Relians) Again (Shortest way Dijstra)

Source: Internet
Author: User

Topic Links:

pku:http://poj.org/problem?id=3653

zju:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemid=1934

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=2722


Description

The 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 Grid of blocks, where each block was a square measuring 2520 rels per side (a rel is the Gorelian official Unit of Distanc e). The speed limit between-adjacent intersections is all constant, and may range from 1 to 9 rels per blip (a blip , of 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, 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 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.

Input

The 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 was a single block, or 1 by 1, and the largest city was a by-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 was 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.

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 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

Source

Mid-Central USA 2007


PS:

Test instructions is more disgusting, the input is more disgusting, but also to divide four directions!

Deal with the input, in a casual use of a short-circuit is good!


The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace    STD; #define INF 0x3f3f3f3fconst int maxn = 547;int mapp[maxn][maxn];int Dijkstra (int start,int n) {int DIS[MAXN];    int MARK[MAXN];    int k, Min;    memset (Mark,0,sizeof (Mark));    for (int i = 1; I <= n; i++) dis[i] = INF;    Dis[start] = 0;        for (int i = 1; I <= n; i++) {min = INF; for (int j = 1; J <= N; j + +) {if (!mark[j] && dis[j]<min) {Min=d                IS[J];            K=j;        }} Mark[k] = 1; for (int j = 1; J <= N; j + +) {if (mapp[k][j]!=0) {if (Dis[j] > Dis[k]+map            P[K][J]) dis[j] = Dis[k]+mapp[k][j]; }}} return dis[n];}    int main () {int V, h, W;    Char dir;        while (~SCANF ("%d%d", &v,&h)) {if (v==0 && h==0) break; MeMset (mapp,0,sizeof (MAPP));        int A, B; for (int i = 0, I <= v; i++) {for (int j = 1; J <= H; j + +) {scanf ("%d%c                ", &w,&dir);                if (w = = 0) continue;                A = i* (h+1) +j;                b = a+1;                int ti = 2520/w;                if (dir = = ' * ') mapp[a][b] = mapp[b][a] = ti;                else if (dir = = ' > ') mapp[a][b] = Ti;            else mapp[b][a] = ti;                    if (I!=V)//is not the last line {for (int j = 1; J <= H+1, J + +)//One more {                    scanf ("%d%c", &w,&dir);                    if (w = = 0) continue;                    A = i* (h+1) +j;                    b = a+h+1;                    int ti = 2520/w;                    if (dir = = ' * ') mapp[a][b] = mapp[b][a] = ti; else if (dir == ' V ') mapp[a][b] = Ti;                else mapp[b][a] = ti;        }}} int tt = (v+1) * (h+1);        int ans = Dijkstra (1,TT);        if (ans! = INF) printf ("%d blips\n", ans);    else printf ("holiday\n"); } return 0;}


POJ 3653 & ZOJ 2935 & HDU 2722 here We Go (Relians) Again (Shortest way Dijstra)

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.