欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

杭电2722(Floyd最短路+麻烦处理输入)

程序员文章站 2022-04-06 18:46:19
...

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.)
杭电2722(Floyd最短路+麻烦处理输入)

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

思路:

方法:

  • 输入按照横、纵、横、纵、横、… 的顺序给出
  • 对输入的那些东西建图
  • 算时间用2520除以速度num
  • 用Floyd算法求最短路

建图:

  • 顶点数量为(n+1)*(m+1)
  • 共n*2+1行数据,横行 n+1 行,纵行 n 行,从0开始标号,即 0 到 n * 2
  • 横路m条路,对应偶数行m个数m个符号
  • 纵路m+1条路,对应奇数行m+1个数m+1个符号

横纵坐标(a,b)的求法

  • 按照从第一行到最后一行,从左向右的顺序为顶点编号
  • 图的描述是按照横、纵的顺序给出的
  • 所以每过两组数据,第一对坐标的横坐标会增大,如:从0增大到3,3增大到6,增大的幅度是m+1, - 每组内横坐标依次加1,如:0到1到2,增大的幅度是 j,所以横坐标为 ( i / 2 ) * ( m + 1 ) + j
  • 横路和纵路的坐标中,纵坐标与横坐标的差值不同,在横路中,对应输入的偶数行,每对横纵坐标差值为1,如:(0,1),(1,2);在纵路中,对应输入的奇数行,每对横纵坐标差值为m + 1,如:(0,3),(1,4),(2,5)

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

#define INF 0xfffffff
#define maxn 1005

int path[maxn][maxn];
int n,m,num;//n纵行,m横行,每条路速度为num
int nn,mm;//nn个顶点,每行mm个数mm个符号
char str;//代表路走向的符号

void floyd()
{
    for(int k=0;k<nn;k++)
    {
        for(int i=0;i<nn;i++)
        {
            if(path[i][k]!=INF)
            {
                for(int j=0;j<maxn;j++)
                {
                    if(path[i][j]>path[i][k]+path[k][j])
                    {
                        path[i][j]=path[i][k]+path[k][j];
                    }
                }
            }
        }
    }
}

int main()
{
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break;
        nn=(n+1)*(m+1);//顶点数量 
        //init
        for(int i=0;i<nn;i++)
        {
            for(int j=0;j<nn;j++)
            {
                if(i==j) path[i][j]=0;
                else path[i][j]=INF;
            }
        }
        //共n*2+1组数据 
        for(int i=0;i<n*2+1;i++)
        {
            //偶数行m个数,奇数行m+1个数 
            if(i%2!=0) mm=m+1;
            else mm=m;
            //每行的输入
            for(int j=0;j<mm;j++)
            {
                int a,b;//横纵坐标(a,b)
                cin>>num>>str;
                /*
                  按照横行、纵行的方式建图 
                  偶数行对应横路 m对结点
                  奇数行对应纵路 m+1对结点 
                */ 
                a=(i/2)*(m+1)+j;//横坐标
                if(mm==m+1) b=a+m+1;//纵坐标
                //或者if(i%2!=0) b=a+m+1;
                else b=a+1;
                if(num!=0)
                {
                    num=2520/num;//时间 
                    //*东西连通 
                    if(str=='*')
                    {
                        path[a][b]=path[b][a]=num;
                    }
                    //>西到东连通 
                    else if(str=='>')
                    {
                        path[a][b]=num;
                    }
                    //<东到西连通 
                    else if(str=='<')
                    {
                        path[b][a]=num;
                    }
                    //v北到南连通 
                    else if(str=='v')
                    {
                        path[a][b]=num;
                    }
                    //^南到北连通 
                    else
                    {
                        path[b][a]=num;
                    }
                }
            }
        }
        floyd();
        if(path[0][nn-1]!=INF)
        {
            cout<<path[0][nn-1]<<" blips"<<endl;
        }
        else cout<<"Holiday"<<endl;
    }
    return 0;
}
相关标签: ACM 最短路径