HDU 2722 Here We Go(relians) Again

sgeteternal · · 1404 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Here We Go(relians) Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 162    Accepted Submission(s): 99


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

Source
 

Recommend
teddy
 
大家米睇条题目甘长,其实系多左一维既最短路水题…………
重点就系控制输入下标同埋储存方式……之后Dijkstra, Bellmen-Ford, Floyd, SPFA, A*随便用就可以AC。
今次惊奇甘样到左Rank5…………真系水到恒。尴尬
 
5 10SGetEternal{(。) 0MS 224K 2043B C++ 2011-08-01 23:54:06
4309587 2011-08-01 23:54:06 Accepted 2722 0MS 224K 2043 B C++ 10SGetEternal{(。)(。)}!
 
 
跟住就系一段比较长既代码,坐标控制慢慢睇吧,我都纠结左好耐嘎啦:
 
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define MAXI 22
#define INFI 0x7fffffff

struct node
{
    int x, y, v; 
    node (int tx = 0, int ty = 0, int tv = 0)
    { x = tx; y = ty; v = tv; }
};
struct graph
{ 
    vector<node> next;
    void push(int x, int y, int v)
    { next.push_back(node(x, y, v)); }
} G[MAXI][MAXI];
int n, m, dis[MAXI][MAXI];
bool inQ[MAXI][MAXI];

void init()
{
    int i, j;

    for (i = 0; i < n + 1; i++)
        for (j = 0; j < m + 1; j++)
        {
            dis[i][j] = INFI;
            inQ[i][j] = 0;
            G[i][j].next.clear();
        }
}

void SPFA(int ux, int uy)
{
    int i;
    node tmp, vtm;
    queue<node> Q;

    tmp.x = ux; tmp.y = uy;
    Q.push(tmp); 
    dis[tmp.x][tmp.y] = 0;
    inQ[tmp.x][tmp.y] = 1;
    while (!Q.empty())
    {
        tmp = Q.front();
        Q.pop();
        inQ[tmp.x][tmp.y] = 0;
        for (i = 0; i < G[tmp.x][tmp.y].next.size(); i++)
        {
            vtm = G[tmp.x][tmp.y].next[i];
            if (dis[vtm.x][vtm.y] - vtm.v > dis[tmp.x][tmp.y])
            {
                dis[vtm.x][vtm.y] = dis[tmp.x][tmp.y] + vtm.v;
                if (!inQ[vtm.x][vtm.y])
                {
                    Q.push(vtm);
                    inQ[vtm.x][vtm.y] = 1;
                }
            }
        }
    }
    if (dis[n][m] == INFI) dis[n][m] = 0;

}

int main()
{
    int i, j, k, v;
    char s;

    while (scanf("%d%d", &n, &m), n || m)
    {
        init();
        for (i = 0; i < 2 * n + 1; i++)
        {
            k = i / 2;
            if (i % 2) m++;
            else m--;
            for (j = 0; j < m + 1; j++)
            {
                scanf("%d%s", &v, &s);
                if (v == 0) continue;
                v = 2520 / v;
                if (s == '*')
                {
                    if (i % 2)
                    {
                        G[k][j].push(k + 1, j, v);
                        G[k + 1][j].push(k, j, v);
                    }
                    else
                    {
                        G[k][j].push(k, j + 1, v);
                        G[k][j + 1].push(k, j, v);
                    }
                }
                if (s == '^') G[k + 1][j].push(k, j, v);
                if (s == 'v') G[k][j].push(k + 1, j, v);
                if (s == '<') G[k][j + 1].push(k, j, v);
                if (s == '>') G[k][j].push(k, j + 1, v);
            }
        }
        m++;
        SPFA(0, 0);
        if (dis[n][m]) printf("%d blips\n", dis[n][m]);
        else printf("Holiday\n");
    }

    return 0;
}

题目长,吾一定唔系水题…………
 

有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:sgeteternal

查看原文:HDU 2722 Here We Go(relians) Again

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1404 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传