河的两岸共有六种点,分别对则六种点进行搜索就行,需要用优先队列优化
刚刚做的时候没想到将河两岸的点放到一个数组里,做的太麻烦了,各种错
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define LL long long
using namespace std;
#define N 1000010
struct Node
{int id;LL val;bool operator <(const Node & a) const{return val > a.val;}
} next,tmp;priority_queue<Node> Q;
LL dis[2*N],ferry[N];
bool vis[N*2];
int n, st, t, x, y;
void relax(int k,LL dis)
{next.id=k;next.val=tmp.val+dis;Q.push(next);
}LL bfs()
{memset(vis, 0, sizeof(vis));while(!Q.empty())Q.pop();tmp.id=st;tmp.val=0;Q.push(tmp);while(!Q.empty()){tmp=Q.top();Q.pop();if(tmp.id==t)return tmp.val;if(vis[tmp.id])continue;vis[tmp.id]=1;if(tmp.id==0){relax(n+1,ferry[0]);relax(1,dis[1]);}else if(tmp.id==n+1){relax(0,ferry[0]);relax(tmp.id+1,dis[tmp.id+1]);}else if(tmp.id==n){relax(tmp.id-1,dis[tmp.id]);relax(tmp.id+n+1,ferry[n]);}else if(tmp.id==2*n+1){relax(N,ferry[n]);relax(tmp.id-1,dis[tmp.id]);}else if(tmp.id>0 && tmp.id <n){relax(tmp.id-1,dis[tmp.id]);relax(tmp.id+1,dis[tmp.id+1]);relax(tmp.id+n+1,ferry[tmp.id]);}else if(tmp.id>n+1 && tmp.id<2*n+1){relax(tmp.id-1,dis[tmp.id]);relax(tmp.id+1,dis[tmp.id+1]);relax(tmp.id-n-1,ferry[tmp.id-n-1]);}}return -1;
}int main(void)
{
// freopen("1.txt", "r", stdin);while(cin>>n && n){cin>>x>>y;st = y+x*(n+1);cin>>x>>y;t = y+x*(n+1);for(int i=1; i<=n; i++)scanf("%lld", &dis[i]);for(int i=0; i<=n; i++)scanf("%lld", &ferry[i]);for(int i=1; i<=n; i++)scanf("%lld", &dis[n+i+1]);cout<<bfs()<<endl;}return 0;
}