1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include<bits/stdc++.h> using namespace std;
#define int long long
void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; }
void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double #define ll long long
#define pii pair<int,int > #define vec vector<int >
#define pb push_back #define mp make_pair #define fr first #define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 1.26e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7;
const int dx[] = {0,1,-1,0,0}; const int dy[] = {0,0,0,1,-1};
int n,m,a,b,c,vis[502][502],w[502][502],s,t,head[maxn],tot,dis[maxn]; struct edge{int to,nxt,w;}e[maxn<<2];
void ins(int u,int v,int w) {e[++tot]=(edge){v,head[u],w},head[u]=tot;}
int p(int x,int y,int d) {return (d-1)*n*m+(x-1)*m+y;}
int check(int x,int y) {return x<=n&&x>0&&y<=m&&y>0;}
void init() { memset(w,63,sizeof w); int T;read(T);queue<pii > q; for(int i=1,x,y;i<=T;i++) { read(x),read(y);x++,y++; if(i==1) s=p(x,y,5); if(i==T) t=p(x,y,5); if(!vis[x][y]) q.push(mp(x,y)); vis[x][y]=1,w[x][y]=0; } while(!q.empty()) { int x=q.front().fr,y=q.front().sc;q.pop(); for(int i=1;i<=4;i++) if(w[x+dx[i]][y+dy[i]]>w[x][y]+1&&check(x+dx[i],y+dy[i])) w[x+dx[i]][y+dy[i]]=w[x][y]+1,q.push(mp(x+dx[i],y+dy[i])); } }
void build() { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(i-1) ins(p(i,j,1),p(i-1,j,1),a),ins(p(i,j,5),p(i-1,j,5),c); if(j-1) ins(p(i,j,2),p(i,j-1,2),a),ins(p(i,j,5),p(i,j-1,5),c); if(i<n) ins(p(i,j,3),p(i+1,j,3),a),ins(p(i,j,5),p(i+1,j,5),c); if(j<m) ins(p(i,j,4),p(i,j+1,4),a),ins(p(i,j,5),p(i,j+1,5),c); for(int x=1;x<=4;x++) ins(p(i,j,x),p(i,j,5),w[i][j]*c),ins(p(i,j,5),p(i,j,x),b); } }
void dijkstra() { memset(dis,63,sizeof dis);dis[s]=0; priority_queue<pii > q;q.push(mp(0,s)); while(!q.empty()) { int x=q.top().sc,www=-q.top().fr;q.pop(); if(www>dis[x]) continue; for(int i=head[x];i;i=e[i].nxt) if(dis[e[i].to]>dis[x]+e[i].w) dis[e[i].to]=dis[x]+e[i].w,q.push(mp(-dis[e[i].to],e[i].to)); } }
signed main() { read(n),read(m),read(a),read(b),read(c);n++,m++; init();build();dijkstra();write(dis[t]); return 0; }
|