HDU 2722 Here We Go(relians) Again

来源:互联网 发布:sql server 2005登录 编辑:程序博客网 时间:2024/05/20 17:25

Here We Go(relians) Again

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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 would 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, 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 should 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 would be 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 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 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, a less-than symbol ‘<’ which indicates travel is 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, an asterisk again indicates travel is allowed in either direction, a lowercase “vee” character ‘v’ indicates travel is allowed only in a north-to-south directions, and a caret symbol ‘^’ indicates travel is allowed only in a south-to-north direction. 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 29 * 9 *6 v 0 * 8 v3 * 7 *3 * 6 v 3 *4 * 8 *2 29 * 9 *6 v 9 * 8 v3 * 7 *3 * 6 v 3 *4 * 8 *2 29 * 9 *6 ^ 0 * 8 ^3 * 7 *3 * 6 ^ 3 *4 * 8 *0 0

Sample Output

1715 blips1295 blipsHoliday


题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2722

分析

题意:规定1单位距离为2520blip,给出一个矩阵,纵向长n个单位,横向长m个单位的矩阵,每两个交叉点之间的都有一个限制速度,输入数据中给出,计算从矩阵左上角到矩阵右下角的最短时间

思路:令人崩溃的一道题,题目长,难理解,数据又难理解,构造图形又麻烦,不过还好是简单的模板题,图构造出来后直接套模板就过了,笑哭。。。

输入数据可能不好理解,看下面这张图应该就明白了,第一组数据的图
这里写图片描述
每个数字跟一个字符是一组,速度表示该条路上的限制速度,字符表示该条路线可以行驶的方向,* 代表双向,> 代表只能向右,< 代表只能向左,v 代表只能向下,^ 代表只能向上
我们可以将地图上各个点标号,我是按照从左到右,从上到下的顺序标上序号0~((n+1)*(m+1)-1);然后根据行列关系,将各个速度转换成行驶2520blip需要的时间,即2520/速度,存入该路径,存好之后调用dijkstra然后输出就行了

做这道题和写这篇博客累得半死,祭出神图休息休息。。。
这里写图片描述

代码

#include<bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace std;const int maxn=450;int n,m;int vis[maxn],cast[maxn],maps[maxn][maxn];void dijkstra(){    int i,j,minn,pos;    memset(vis,0,sizeof(vis));    vis[0]=1;    for(i=1; i<=(n+1)*(m+1)-1; i++)        cast[i] = maps[0][i];    for(i=1; i<=(n+1)*(m+1)-1; i++)    {        minn=INF;        for(j=1; j<=(n+1)*(m+1)-1; j++)        {            if(cast[j]<minn&&!vis[j])            {                pos=j;                minn=cast[j];            }        }        vis[pos]=1;        for(j=1; j<=(n+1)*(m+1)-1; j++)        {            if(cast[pos]+maps[pos][j]<cast[j]&&!vis[j])                cast[j]=cast[pos]+maps[pos][j];        }    }}int main(){    while(~scanf("%d%d",&n,&m)&&(n||m))    {        for(int i=0; i<maxn; i++)        {            for(int j=0; j<maxn; j++)                maps[i][j]=INF;            maps[i][i]=0;        }        for(int i=0; i<2*n+1; i++)///i表示的是输入的行数,i/2为路径端点对应的行数        {            int time;            char dir;            int line=i/2;            ///i为偶数输入的是横向边            if(i%2==0)            {                for(int j=0; j<m; j++)                {                    scanf("%d %c",&time,&dir);                    if(time)                    {                        time=2520/time;///将速度转换为时间,然后再存放                        if(dir=='*')                        {                            maps[(m+1)*line+j][(m+1)*line+j+1]=min(maps[(m+1)*line+j][(m+1)*line+j+1],time);                            maps[(m+1)*line+j+1][(m+1)*line+j]=min(maps[(m+1)*line+j+1][(m+1)*line+j],time);                        }                        if(dir=='>')maps[(m+1)*line+j][(m+1)*line+j+1]=min(maps[(m+1)*line+j][(m+1)*line+j+1],time);                        if(dir=='<')maps[(m+1)*line+j+1][(m+1)*line+j]=min(maps[(m+1)*line+j+1][(m+1)*line+j],time);                        ///根据行列关系将maps的值更新                    }                }            }            ///i为奇数输入的是纵向边            else            {                for(int j=0; j<m+1; j++)                {                    scanf("%d %c",&time,&dir);                    if(time)                    {                        time=2520/time;                        if(dir=='*')                        {                            maps[(m+1)*line+j][(m+1)*(line+1)+j]=min(maps[(m+1)*line+j][(m+1)*(line+1)+j],time);                            maps[(m+1)*(line+1)+j][(m+1)*line+j]=min(maps[(m+1)*(line+1)+j][(m+1)*line+j],time);                        }                        if(dir=='v')maps[(m+1)*line+j][(m+1)*(line+1)+j]=min(maps[(m+1)*line+j][(m+1)*(line+1)+j],time);                        if(dir=='^')maps[(m+1)*(line+1)+j][(m+1)*line+j]=min(maps[(m+1)*(line+1)+j][(m+1)*line+j],time);                    }                }            }        }        dijkstra();        if(cast[(n+1)*(m+1)-1]!=INF)printf("%d blips\n",cast[(n+1)*(m+1)-1]);        else printf("Holiday\n");    }}
原创粉丝点击