「UOJ184」[ZJOI2016]旅行者
分治+最短路
写spfa会T。。
1 #include<bits/stdc++.h> 2 #define R register 3 using namespace std; 4 const int N=40010,M=100010,oo=2e9; 5 const int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; 6 int read(){ 7 int x=0,w=1;char c=0; 8 while(c<'0'||c>'9'){if(c=='-') w=-1;c=getchar();} 9 while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar(); 10 return x*w; 11 } 12 struct P{int x,y;}; 13 struct Node{ 14 int x,y,d; 15 bool operator<(Node k)const{return d>k.d;} 16 }; 17 int n,m,q,a[N],b[N]; 18 int qans[M],dis[N]; 19 int vis[N],fix[N],timer; 20 int qid[M],tid[M]; 21 P qs[M],qt[M]; 22 inline int getdis(int x,int y,int dir){ 23 if(dir==0) return b[(x-1)*m+y]; 24 else if(dir==1) return a[x*m+y]; 25 else if(dir==2) return b[x*m+y]; 26 else return a[x*m+y-1]; 27 } 28 inline int getid(register int x,register int y){return x*m+y;} 29 //inline void spfa(int sx,int sy,int x1,int y1,int x2,int y2){ 30 // timer++; 31 // vis[getid(sx,sy)]=timer; 32 // dis[getid(sx,sy)]=0; 33 // queue<P>q; 34 // q.push(P(sx,sy)); 35 // P now,nxt; 36 // while(!q.empty()){ 37 // now=q.front();q.pop(); 38 // for(int i=0;i<4;i++){ 39 // nxt=P(now.x+dx[i],now.y+dy[i]); 40 // if(nxt.x>=x1&&nxt.x<=x2&&nxt.y>=y1&&nxt.y<=y2){ 41 // int nxtid=getid(nxt.x,nxt.y); 42 // if(vis[nxtid]!=timer){ 43 // vis[nxtid]=timer,dis[nxtid]=dis[getid(now.x,now.y)]+getdis(now.x,now.y,i); 44 // q.push(nxt); 45 // }else if(dis[getid(now.x,now.y)]+getdis(now.x,now.y,i)<dis[nxtid]){ 46 // dis[nxtid]=dis[getid(now.x,now.y)]+getdis(now.x,now.y,i); 47 // q.push(nxt); 48 // } 49 // } 50 // } 51 // } 52 // return; 53 //} 54 inline void dij(int sx,int sy,int x1,int y1,int x2,int y2){ 55 priority_queue<Node>q; 56 timer++; 57 vis[getid(sx,sy)]=timer; 58 dis[getid(sx,sy)]=0; 59 q.push((Node){sx,sy,0}); 60 P now,nxt; 61 Node xx; 62 while(!q.empty()){ 63 xx=q.top();q.pop(); 64 if(fix[getid(xx.x,xx.y)]==timer||vis[getid(xx.x,xx.y)]==timer&&dis[getid(xx.x,xx.y)]<xx.d) continue; 65 fix[getid(xx.x,xx.y)]=timer; 66 now.x=xx.x,now.y=xx.y; 67 for(int i=0;i<4;i++){ 68 nxt=(P){now.x+dx[i],now.y+dy[i]}; 69 if(nxt.x>=x1&&nxt.x<=x2&&nxt.y>=y1&&nxt.y<=y2){ 70 int nxtid=getid(nxt.x,nxt.y); 71 if(vis[nxtid]!=timer){ 72 vis[nxtid]=timer,dis[nxtid]=dis[getid(now.x,now.y)]+getdis(now.x,now.y,i); 73 q.push((Node){nxt.x,nxt.y,dis[nxtid]}); 74 }else if(dis[getid(now.x,now.y)]+getdis(now.x,now.y,i)<dis[nxtid]){ 75 dis[nxtid]=dis[getid(now.x,now.y)]+getdis(now.x,now.y,i); 76 q.push((Node){nxt.x,nxt.y,dis[nxtid]}); 77 } 78 } 79 } 80 } 81 return; 82 } 83 void solve(int x1,int y1,int x2,int y2,int ql,int qr){ 84 if(x2<x1||y2<y1||ql>qr) return; 85 // cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<ql<<" "<<qr<<endl; 86 int pal=ql-1,par=qr+1; 87 if(x2-x1>=y2-y1){ 88 int mid=(x1+x2)>>1; 89 for(int j=ql;j<=qr;j++){ 90 int nowq=qid[j]; 91 if(qs[nowq].x>=x1&&qs[nowq].x<=mid-1&&qt[nowq].x>=x1&&qt[nowq].x<=mid-1) tid[++pal]=nowq; 92 else if(qs[nowq].x>=mid+1&&qs[nowq].x<=x2&&qt[nowq].x>=mid+1&&qt[nowq].x<=x2) tid[--par]=nowq; 93 } 94 for(int i=y1;i<=y2;i++){ 95 dij(mid,i,x1,y1,x2,y2); 96 for(int j=ql;j<=qr;j++){ 97 int nowq=qid[j]; 98 if(qs[nowq].x>=x1&&qs[nowq].x<=x2&&qs[nowq].y>=y1&&qs[nowq].y<=y2&&qt[nowq].x>=x1&&qt[nowq].x<=x2&&qt[nowq].y>=y1&&qt[nowq].y<=y2) 99 qans[nowq]=min(qans[nowq],dis[getid(qs[nowq].x,qs[nowq].y)]+dis[getid(qt[nowq].x,qt[nowq].y)]); 100 } 101 } 102 for(R int i=ql;i<=pal;i++) qid[i]=tid[i]; 103 for(R int i=qr;i>=par;i--) qid[i]=tid[i]; 104 if(pal>=ql) solve(x1,y1,mid-1,y2,ql,pal); 105 if(par<=qr) solve(mid+1,y1,x2,y2,par,qr); 106 }else{ 107 int mid=(y1+y2)>>1; 108 for(int j=ql;j<=qr;j++){ 109 int nowq=qid[j]; 110 if(qs[nowq].y>=y1&&qs[nowq].y<=mid-1&&qt[nowq].y>=y1&&qt[nowq].y<=mid-1) tid[++pal]=nowq; 111 else if(qs[nowq].y>=mid+1&&qs[nowq].y<=y2&&qt[nowq].y>=mid+1&&qt[nowq].y<=y2) tid[--par]=nowq; 112 } 113 for(int i=x1;i<=x2;i++){ 114 dij(i,mid,x1,y1,x2,y2); 115 for(int j=ql;j<=qr;j++){ 116 int nowq=qid[j]; 117 if(qs[nowq].x>=x1&&qs[nowq].x<=x2&&qs[nowq].y>=y1&&qs[nowq].y<=y2&&qt[nowq].x>=x1&&qt[nowq].x<=x2&&qt[nowq].y>=y1&&qt[nowq].y<=y2) 118 qans[nowq]=min(qans[nowq],dis[getid(qs[nowq].x,qs[nowq].y)]+dis[getid(qt[nowq].x,qt[nowq].y)]); 119 } 120 } 121 for(R int i=ql;i<=pal;i++) qid[i]=tid[i]; 122 for(R int i=qr;i>=par;i--) qid[i]=tid[i]; 123 if(pal>=ql) solve(x1,y1,x2,mid-1,ql,pal); 124 if(par<=qr) solve(x1,mid+1,x2,y2,par,qr); 125 } 126 return; 127 } 128 int main(){ 129 int t1,t2,t3,t4; 130 memset(qans,127/2,sizeof(qans)); 131 n=read(),m=read(); 132 for(R int i=0;i<n;i++) 133 for(int j=0;j<m-1;j++) a[getid(i,j)]=read(); 134 for(R int i=0;i<n-1;i++) 135 for(int j=0;j<m;j++) b[getid(i,j)]=read(); 136 q=read(); 137 for(R int i=1;i<=q;i++) t1=read(),t2=read(),t3=read(),t4=read(),qs[i]=(P){t1-1,t2-1},qt[i]=(P){t3-1,t4-1},qid[i]=i; 138 solve(0,0,n-1,m-1,1,q); 139 for(R int i=1;i<=q;i++) printf("%d\n",qans[i]); 140 return 0; 141 }