分析
考虑对网格图进行分治。
假设当前分治的范围是 $(lx,ly)\sim(rx,ry)$ ,询问的范围是 $ql\sim qr$ 。
找到矩形范围的长边,然后用一条中轴线切成两半。
这时每个询问有两种情况:
- 查询的两点不在中轴线同侧,此时最短路一定经过了中轴线上的某个点。那么对于中轴线上的每一个点,跑最短路,然后更新询问区间内每个询问的答案。
- 查询的两点在中轴线同侧,此时最短路可能经过中轴线上的某个点。于是与上面一样的处理,然后递归下去处理即可。
时间复杂度据说是 $\mathcal{O}(S\sqrt{S}\log S)$ ,其中 $S=nm$,然而我并不会证。
代码
// ===================================
// author: M_sea
// website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#define re register
using namespace std;
template<typename T>
inline void chkmin(T& x,T y) { if (y<x) x=y; }
inline int read() {
int X=0,w=1; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
return X*w;
}
const int N=20000+10;
const int M=100000+10;
struct Edge { int v,w,nxt; } e[N<<2];
int head[N];
int n,m,Q;
int dis[N],x[N],y[N],ans[M];
struct Query { int id,s,t; } q[M],lq[M],rq[M];
inline int id(int x,int y) { return (x-1)*m+y; }
inline void addEdge(int u,int v,int w) {
static int cnt=0;
e[++cnt]=(Edge){v,w,head[u]},head[u]=cnt;
}
struct node { int u,d; };
bool operator <(node a,node b) { return a.d>b.d; }
inline void dijkstra(int s,int lx,int ly,int rx,int ry) {
for (re int i=lx;i<=rx;++i)
for (re int j=ly;j<=ry;++j)
dis[id(i,j)]=1e9;
priority_queue<node> Q; Q.push((node){s,0}); dis[s]=0;
while (!Q.empty()) {
node tp=Q.top(); Q.pop();
int u=tp.u,d=tp.d;
if (dis[u]!=d) continue;
for (re int i=head[u];i;i=e[i].nxt) {
int v=e[i].v,w=e[i].w;
if (x[v]<lx||x[v]>rx||y[v]<ly||y[v]>ry) continue;
if (d+w<dis[v]) dis[v]=d+w,Q.push((node){v,dis[v]});
}
}
}
inline void solve(int ql,int qr,int lx,int ly,int rx,int ry) {
if (ql>qr) return;
if (rx-lx>=ry-ly) {
int mid=(lx+rx)>>1;
for (re int i=ly;i<=ry;++i) {
dijkstra(id(mid,i),lx,ly,rx,ry);
for (re int j=ql;j<=qr;++j)
chkmin(ans[q[j].id],dis[q[j].s]+dis[q[j].t]);
}
int ls=0,rs=0;
for (re int i=ql;i<=qr;++i) {
int s=q[i].s,t=q[i].t;
if (x[s]<mid&&x[t]<mid) lq[++ls]=q[i];
if (x[s]>mid&&x[t]>mid) rq[++rs]=q[i];
}
for (re int i=1,j=ql;i<=ls;++i,++j) q[j]=lq[i];
for (re int i=1,j=ql+ls;i<=rs;++i,++j) q[j]=rq[i];
solve(ql,ql+ls-1,lx,ly,mid-1,ry);
solve(ql+ls,ql+ls+rs-1,mid+1,ly,rx,ry);
} else {
int mid=(ly+ry)>>1;
for (re int i=lx;i<=rx;++i) {
dijkstra(id(i,mid),lx,ly,rx,ry);
for (re int j=ql;j<=qr;++j)
chkmin(ans[q[j].id],dis[q[j].s]+dis[q[j].t]);
}
int ls=0,rs=0;
for (re int i=ql;i<=qr;++i) {
int s=q[i].s,t=q[i].t;
if (y[s]<mid&&y[t]<mid) lq[++ls]=q[i];
if (y[s]>mid&&y[t]>mid) rq[++rs]=q[i];
}
for (re int i=1,j=ql;i<=ls;++i,++j) q[j]=lq[i];
for (re int i=1,j=ql+ls;i<=rs;++i,++j) q[j]=rq[i];
solve(ql,ql+ls-1,lx,ly,rx,mid-1);
solve(ql+ls,ql+ls+rs-1,lx,mid+1,rx,ry);
}
}
int main() {
n=read(),m=read();
for (re int i=1;i<=n;++i)
for (re int j=1;j<=m;++j)
x[id(i,j)]=i,y[id(i,j)]=j;
for (re int i=1;i<=n;++i)
for (re int j=1;j<m;++j) {
int w=read();
addEdge(id(i,j),id(i,j+1),w);
addEdge(id(i,j+1),id(i,j),w);
}
for (re int i=1;i<n;++i)
for (re int j=1;j<=m;++j) {
int w=read();
addEdge(id(i,j),id(i+1,j),w);
addEdge(id(i+1,j),id(i,j),w);
}
int Q=read();
for (re int i=1;i<=Q;++i) {
int lx=read(),ly=read(),rx=read(),ry=read();
q[i]=(Query){i,id(lx,ly),id(rx,ry)};
}
memset(ans,0x3f,sizeof(ans)); solve(1,Q,1,1,n,m);
for (re int i=1;i<=Q;++i) printf("%d\n",ans[i]);
return 0;
}