Codevs1364尋寶題解

來源:互聯網
上載者:User

標籤:圖論   dijkstra   堆最佳化   最短路   

http://codevs.cn/problem/1364/

  • 題解
    一看就是一道最短路的題。設起點、終點,按題意一條條地添邊。每層樓都是環狀的,終點在第N+1層,添邊時要格外小心。有點分層圖的意思。堆最佳化dijkstra耐心寫下去。本題考最短路,還考耐心。

  • Code

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int oo = 1000000000, nil = 0;int N, M, map[65][65];struct Point{    int h, n;    Point(int h = 0, int n = 0) :h(h), n(n) {}};Point u[20000], v[20000], S, T;int e, w[20000], nxt[20000], pnt[65][65];int d[65][65];bool can[65][65], vis[65][65];void addedge(Point a, Point b, int c){    u[++e] = a; v[e] = b; w[e] = c;    nxt[e] = pnt[a.h][a.n]; pnt[a.h][a.n] = e;}void init(){    int opt;    memset(can, 0, sizeof(can));    memset(w, 0, sizeof(w));    memset(nxt, 0, sizeof(nxt));    memset(pnt, 0, sizeof(pnt));    e = 0;    scanf("%d%d", &N, &M);    for(int i = 0; i < N; ++i)    {        for(int j = 0; j < M; ++j)        {            scanf("%d%d", &opt, &map[i][j]);            if(opt == 1)            {                can[i][j] = true;            }        }    }    for(int i = 0; i < N; ++i)    {        for(int j = 0; j < M; ++j)        {            addedge(Point(i, j), Point(i, (j + 1) % M), map[i][(j + 1) % M]);            addedge(Point(i, (j + 1) % M), Point(i, j), map[i][j]);            opt = j - 1;            if(opt < 0)            {               opt = M - 1;            }            addedge(Point(i, j), Point(i, opt), map[i][opt]);            addedge(Point(i, opt), Point(i, j), map[i][j]);            if(can[i][j])            {                addedge(Point(i, j), Point(i + 1, j), map[i + 1][j]);            }        }    }    S = Point(N - 1, M);    T = Point(N, M);    for(int i = 0; i < M; ++i)    {        addedge(S, Point(0, i), map[0][i]);    }    for(int i = 0; i < M; ++i)    {        addedge(Point(N, i), T, 0);    }}struct node : public Point{    int d;    node(int xh = 0, int xn = 0, int xd = 0)    {        h = xh; n = xn; d = xd;    }    bool operator < (const node& b) const    {        return d > b.d;    }};void work(){    memset(d, 0x3f, sizeof(d));    memset(vis, 0, sizeof(vis));    priority_queue <node> Q;    d[S.h][S.n] = 0;    vis[S.h][S.n] = 1;    Q.push(node(S.h, S.n, 0));    while(!Q.empty())    {        node t = Q.top();        Q.pop();        for(int j = pnt[t.h][t.n]; j != nil; j = nxt[j])        {            if((!vis[v[j].h][v[j].n]) && d[v[j].h][v[j].n] > t.d + w[j])            {                d[v[j].h][v[j].n] = t.d + w[j];                vis[v[j].h][v[j].n] = true;                Q.push(node(v[j].h, v[j].n, d[v[j].h][v[j].n]));            }        }    }    if(d[T.h][T.n] > oo)    {        puts("-1");    }    else    {        printf("%d\n", d[T.h][T.n]);    }}int main(){    init();    work();    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Codevs1364尋寶題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.