poj 3377 Ferry Lanes

终于1a了,啊,好长时间没有这种感觉了,这题没有甚么需要注意得,不然不可能1a

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int Maxn = 1000010;
const __int64 inf = 1999999999;

int n, sx, sy, ex, ey;
int di[][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int w_dis[2][Maxn], lane[Maxn];
__int64 dis[2][Maxn];
bool vis[2][Maxn];

struct node {
int x, y;
__int64 dis;

bool operator < (const node &b)const {
    return dis > b.dis;
}
};

void ini() {
    for(int i = 0; i <= n; ++ i)
    {
     vis[0][i] =  vis[1][i] = false;
     dis[1][i] = dis[0][i] = inf;
    }
}

bool inmap(int x,int y) {
    if(0<=x&&x<=1&&y>=0&&y<=n)
        return true;
    else
        return false;

}

void dij() {
    priority_queue<node> q;
    node now, next;
    now.x = sx, now.y = sy, dis[sx][sy] = 0, now.dis = 0;
    q.push(now);
    while(!q.empty()) {
    now = q.top();
    q.pop();
    if(vis[now.x][now.y])
        continue;
    vis[now.x][now.y] = true;
    if(now.x == ex&&now.y == ey)
        break;
    int x, y;
    for(int i = 0; i < 4; ++i)
    {
        x  = now.x + di[i][0]; y = now.y + di[i][1];
        if(!inmap(x,y))
            continue;
        if(i < 2)//表示上下位移
        {
        if(!vis[x][y] && dis[now.x][now.y] + lane[now.y] < dis[x][y])
        {
            dis[x][y] = dis[now.x][now.y] + lane[now.y];
            next.x = x, next.y = y, next.dis = dis[x][y];
            q.push(next);
        }
        }
        else
        {
            if(i == 2)
            {
                if(!vis[x][y] && dis[now.x][now.y] + w_dis[now.x][y] < dis[x][y])
                {
                    dis[x][y] = dis[now.x][now.y] + w_dis[now.x][y];
                    next.x = x, next.y = y, next.dis = dis[x][y];
                    q.push(next);
                }
            }
            else {
                if(!vis[x][y] && dis[now.x][now.y] + w_dis[now.x][now.y] < dis[x][y])
                {
                    dis[x][y] = dis[now.x][now.y] + w_dis[now.x][now.y];
                    next.x = x, next.y = y, next.dis = dis[x][y];
                    q.push(next);
                }
            }
        }
    }


    }


}

int main(void)
{
    while(scanf("%d",&n),n)
    {
        ini();
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        for(int i = 0; i < n; ++i )
            scanf("%d",&w_dis[0][i]);
        for(int i = 0; i <= n; ++i)
            scanf("%d",&lane[i]);
        for(int i = 0; i < n; i++)
            scanf("%d",&w_dis[1][i]);
            dij();
            cout<<dis[ex][ey]<<endl;
    }
    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值