UVa1078 Steam Roller(Dijkstra)

题意

给出一个图,边值不等于0的表示通过这条路所需要的时间,有如下一些约束

  • 在进入这条边前刚转弯
  • 离开这条边后立即转弯
  • 起点开始的边
  • 终点的边
    时间需要加倍,同时时间加倍不能叠加

问从起点到终点所需要的最短时间

思路

方法1:将一个点拆分成8个点,每个点有(r,c,dir,doubled)表示,表示上一步经过方向dir移动到(r,c)这个点,以及移动到这个点,边的值是否已经加倍。在构造图过程中,计算当前结点的后继结点时,首先看点对应方向是否可以走,在计算时间加倍时,如果到当前节点没有加倍,并且方向与下一步方向不一致,则上一步的边需要加倍,同时下一步的连也需要加倍
在根据遍历(r,c,dir,doubled)时,可以先遍历新的方向,看对应方向是否可行,来优化执行时间
方法2:添加一种静止点,即一个点扩展为5个点。由运动点转变为静止点以及继续运动,边的值不加倍。而静止点转变为继续运动和运动后再静止时边的值需要加倍

代码

方法1代码

#include <bits/stdc++.h>

using namespace std;

#define _for(i, a, b) for(int i = (a); i < (b); i++)
#define _rep(i, a, b) for (int i = (a); i <= (b); i++)

struct Edge
{
    int from, to, dist;
};

struct HeapNode
{
    int u, d;

    bool operator<(const HeapNode& other) const
    {
        return d > other.d;
    }
};



template <size_t SZV, int INF>
struct Dijkstra
{
    int n;
    bool done[SZV];
    vector<Edge> edges;
    vector<int> graph[SZV];
    int d[SZV];
    int p[SZV];

    void init(int n)
    {
        this-> n = n;
        _for(i, 0, n) {
            graph[i].clear();
        }
        edges.clear();
    }

    void addEdge(int from, int to, int d)
    {
        graph[from].push_back(edges.size());
        edges.push_back((Edge){from, to, d});
    }

    void dijkstra(int s)
    {
        priority_queue<HeapNode> pq;
        fill_n(done, n, false);
        fill_n(d, n, INF);
        d[s] = 0;
        pq.push({s, 0});

        while (!pq.empty()) {
            HeapNode curNode = pq.top();
            pq.pop();

            int u = curNode.u;
            if (done[u]) {
                continue;
            }

            done[u] = true;
            _for(i, 0, graph[u].size()) {
                Edge& edge = edges[graph[u][i]];
                if (d[u] + edge.dist < d[edge.to]) {
                    d[edge.to] = d[u] + edge.dist;
                    p[edge.to] = graph[u][i];
                    pq.push((HeapNode){edge.to, d[edge.to]});
                }
            }


        }
    }
};



void fastio()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int maxr = 100;
const int maxc = 100;
const int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
const int inv[] = {2, 3, 0, 1};
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const int MAXN = maxr * maxc * 8 + 2;
const int INF = 1e6;

int R, C;
int r1, c1, r2, c2;
int grid[maxr][maxc][4];
Dijkstra<MAXN, INF> solver;
int id[maxr][maxc][4][2];
int n;


int ID(int r, int c, int dir, int doubled)
{
    int& x= id[r][c][dir][doubled];
    if (x == 0) {
        x = ++n;
    }

    return x;
}

bool canGo(int r, int c, int dir)
{
    if (r < 0 || r >= R || c < 0 || c >= C) {
        return false;
    }

    return grid[r][c][dir] > 0;
}

int main()
{
    fastio();

    #ifndef ONLINE_JUDGE
        ifstream fin("f:\\OJ\\uva_in.txt");
        streambuf* back = cin.rdbuf(fin.rdbuf());
    #endif

    int kase = 1;
    while (cin >> R >> C >> r1 >> c1 >> r2 >> c2) {
        if (R == 0) {
            break;
        }

        --r1; --c1; --r2; --c2;
        _for(r, 0, R) {
            _for(c, 0, C - 1) {
                int num;
                cin >> num;
                grid[r][c][RIGHT] = grid[r][c + 1][LEFT] = num;
            }

            if (r != R - 1) {
                _for(c, 0, C) {
                    int num;
                    cin >> num;
                    grid[r][c][DOWN] = grid[r + 1][c][UP] = num;
                }
            }
        }

        solver.init(R * C * 8 + 1);
        n = 0;
        memset(id, 0x00, sizeof(id));


        _for(dir, 0, 4) {
            if (canGo(r1, c1, dir)) {
                solver.addEdge(0, ID(r1 + dr[dir], c1 + dc[dir], dir, 1), grid[r1][c1][dir] * 2);
            }
        }

        _for(r, 0, R) {
            _for(c, 0, C) {
                _for(dir, 0, 4) {
                    if (!canGo(r, c, inv[dir])) {
                        continue;
                    }
                     _for(newdir, 0, 4) {
                        if (!canGo(r, c, newdir)) {
                                continue;
                            }

                        _for(doubled, 0, 2) {


                            int newr = r + dr[newdir];
                            int newc = c + dc[newdir];
                            int v = grid[r][c][newdir];
                            int newdoubled = 0;

                            if (dir != newdir) {
                                if (!doubled) {
                                    v += grid[r][c][inv[dir]];
                                }
                                newdoubled = 1;
                                v += grid[r][c][newdir];
                            }

                            solver.addEdge(ID(r, c, dir, doubled), ID(newr, newc, newdir, newdoubled), v);
                        }
                    }
                }
            }
        }

        solver.dijkstra(0);

        int ans = INF;
        _for(dir, 0, 4) {
            if (!canGo(r2, c2, inv[dir])) {
                continue;
            }

            _for(doubled, 0, 2) {
                int v = solver.d[ID(r2, c2, dir, doubled)];
                //cout << "v:" << v << endl;
                if (!doubled) {
                    v += grid[r2][c2][inv[dir]];
                }

                ans = min(ans, v);
            }
        }

        cout << "Case " << kase++ << ": ";
        if (ans == INF) {
            cout << "Impossible" << endl;
        } else {
            cout << ans << endl;
        }
    }


    #ifndef ONLINE_JUDGE
        cin.rdbuf(back);
    #endif

    return 0;
}

方法2代码

#include <bits/stdc++.h>

using namespace std;

#define _for(i, a, b) for(int i = (a); i < (b); i++)
#define _rep(i, a, b) for (int i = (a); i <= (b); i++)

struct Edge
{
    int from, to, dist;
};

struct HeapNode
{
    int u, d;

    bool operator<(const HeapNode& other) const
    {
        return d > other.d;
    }
};



template <size_t SZV, int INF>
struct Dijkstra
{
    int n;
    bool done[SZV];
    vector<Edge> edges;
    vector<int> graph[SZV];
    int d[SZV];
    int p[SZV];

    void init(int n)
    {
        this-> n = n;
        _for(i, 0, n) {
            graph[i].clear();
        }
        edges.clear();
    }

    void addEdge(int from, int to, int d)
    {
        graph[from].push_back(edges.size());
        edges.push_back((Edge){from, to, d});
    }

    void dijkstra(int s)
    {
        priority_queue<HeapNode> pq;
        fill_n(done, n, false);
        fill_n(d, n, INF);
        d[s] = 0;
        pq.push({s, 0});

        while (!pq.empty()) {
            HeapNode curNode = pq.top();
            pq.pop();

            int u = curNode.u;
            if (done[u]) {
                continue;
            }

            done[u] = true;
            _for(i, 0, graph[u].size()) {
                Edge& edge = edges[graph[u][i]];
                if (d[u] + edge.dist < d[edge.to]) {
                    d[edge.to] = d[u] + edge.dist;
                    p[edge.to] = graph[u][i];
                    pq.push((HeapNode){edge.to, d[edge.to]});
                }
            }


        }
    }
};



void fastio()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int maxr = 100;
const int maxc = 100;
const int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
const int inv[] = {2, 3, 0, 1};
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const int MAXN = maxr * maxc * 8 + 2;
const int INF = 1e6;

int R, C;
int r1, c1, r2, c2;
int grid[maxr][maxc][4];
Dijkstra<MAXN, INF> solver;
int id[maxr][maxc][5];
int n;


int ID(int r, int c, int dir)
{
    int& x= id[r][c][dir];
    if (x == 0) {
        x = ++n;
    }

    return x;
}

bool canGo(int r, int c, int dir)
{
    if (r < 0 || r >= R || c < 0 || c >= C) {
        return false;
    }

    return grid[r][c][dir] > 0;
}

int main()
{
    fastio();

    #ifndef ONLINE_JUDGE
        ifstream fin("f:\\OJ\\uva_in.txt");
        streambuf* back = cin.rdbuf(fin.rdbuf());
    #endif

    int kase = 1;
    while (cin >> R >> C >> r1 >> c1 >> r2 >> c2) {
        if (R == 0) {
            break;
        }

        --r1; --c1; --r2; --c2;
        _for(r, 0, R) {
            _for(c, 0, C - 1) {
                int num;
                cin >> num;
                grid[r][c][RIGHT] = grid[r][c + 1][LEFT] = num;
            }

            if (r != R - 1) {
                _for(c, 0, C) {
                    int num;
                    cin >> num;
                    grid[r][c][DOWN] = grid[r + 1][c][UP] = num;
                }
            }
        }

        solver.init(R * C * 5 + 1);
        n = 0;
        memset(id, 0x00, sizeof(id));


        _for(dir, 0, 4) {
            if (!canGo(r1, c1, dir)) {
                continue;
            }

            solver.addEdge(0, ID(r1 + dr[dir], c1 + dc[dir], dir), grid[r1][c1][dir] * 2);
            solver.addEdge(0, ID(r1 + dr[dir], c1 + dc[dir], 4), grid[r1][c1][dir] * 2);
        }

        _for(r, 0, R) {
            _for(c, 0, C) {
                _for(dir, 0, 4) {
                    if (!canGo(r, c, inv[dir])) {
                        continue;
                    }

                    solver.addEdge(ID(r, c, dir), ID(r, c, 4), grid[r][c][inv[dir]]);


                    if (!canGo(r, c, dir)) {
                        continue;
                    }

                    solver.addEdge(ID(r, c, dir), ID(r + dr[dir], c + dc[dir], dir), grid[r][c][dir]);
                }

                _for(dir, 0, 4) {
                    if (!canGo(r, c, dir)) {
                        continue;
                    }

                    solver.addEdge(ID(r, c, 4), ID(r + dr[dir], c + dc[dir], dir), grid[r][c][dir] * 2);
                    solver.addEdge(ID(r, c, 4), ID(r + dr[dir], c + dc[dir], 4), grid[r][c][dir] * 2);
                }
            }
        }

        solver.dijkstra(0);

        int ans = solver.d[ID(r2, c2, 4)];
        cout << "Case " << kase++ << ": ";
        if (ans == INF) {
            cout << "Impossible" << endl;
        } else {
            cout << ans << endl;
        }
    }


    #ifndef ONLINE_JUDGE
        cin.rdbuf(back);
    #endif

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值