题解:P16527 [THUPC 2026 决赛] 展览区寻宝
I_am_Accepted
·
·
题解
由于有保证墙壁是四联通的,固定起点 s 终点 t,所有八向行走路径都是拓扑相同的(也就是说,对于任意两条路径 p_1,p_2:p_1 一定能平滑变换至 p_2 而不经过墙壁)
考虑八向移动的代价,这里称之为“度量”,在本题中为 \hat C=(3,2,3,2,3,2,3,2)(从正右方逆时针遍历方向)。如果一个度量:
- 所有方向代价均非负
- 斜向代价不大于对应 横向 + 纵向 代价之和
那这个度量才是合理的,下文仅讨论合理的度量
对于一个度量 Q,从 s 至 t 的八向路径中最小化 Q 代价的路径集合记为 P_Q,最小的代价记为 E_Q
设 \hat H=(1,1,0,1,1,1,0,1)(即,只记录横向移动的代价),我们神奇地(也直觉地)发现 P_{\hat C}\subseteq P_{\hat H}
囿于能力所限,笔者无法严格证明此发现,不过此发现源于“所有路径拓扑相同”的结论。若在 \hat H 中绕路了导致路径并非最优,则在 \hat C 中路径也不是最优
::::warning[假如不保证“所有路径拓扑相同”的反例]{open}
如图,在 \hat H 度量下会绕上面一大圈(因为横向折返少),这在 \hat C 下不是最优的。这是因为上下两条路被中间的墙壁隔开,导致两条路径拓扑不相同
:::align{center}
::::
类似地考虑纵向、斜向、反斜向:
:::align{center}
| 五种度量图示 |
| $$ |
| \begin{aligned} |
| H=&(2,2,0,2,2,2,0,2)=2\hat H\ |
| V=&(0,2,2,2,0,2,2,2)\ |
| D=&(1,2,1,0,1,2,1,0)\ |
| I=&(1,0,1,2,1,0,1,2)\ |
| C=&(4,6,4,6,4,6,4,6)=2\hat C\ |
| P_C\subseteq&P_H\cap P_V\cap P_D\cap P_I\ |
| C=&H+V+D+I\ |
| 2E_{\hat C}=E_C=&E_H+E_V+E_D+E_I |
| \end{aligned} |
| $$ |
$V$ 与 $H$ 仅为旋转 $90^\circ$ 区别;$D,I$ 使用 $(x,y)\mapsto (x-y,x+y)$ 变换应用至地图(旋转 $45^\circ$),再用 $H$ 的方法求解即可(区别是禁用对角线行走)。所以问题只剩 $H$ 如何求解
遍历地图每一列。每一列之中,墙壁将空白区域划分成若干空腔。每个空腔之内移动代价为 $0$,于是用一个节点代表此空腔
遍历地图所有相邻两列 $c_1,c_2$。若 $c_1$ 中的空腔 $x$ 可以直接走到 $c_2$ 中的空腔 $y$,则将节点 $x,y$ 之间连一条边
节点之间的图为森林。节点 $x$ 到 $y$ 的 $H$ 代价即为树上距离
时空复杂度为线性对数
::::success[参考代码]
```cpp
#include<bits/stdc++.h>
using namespace std;
#define rep(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii<=Ii##_;Ii++)
#define per(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii>=Ii##_;Ii--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double db;
#define fir first
#define sec second
#define siz(Aa) ((int)(Aa).size())
#define all(Aa) (Aa).begin(),(Aa).end()
#define ckmx(Aa,Bb) (Aa=max(Aa,Bb))
#define ckmn(Aa,Bb) (Aa=min(Aa,Bb))
const int N=2e5+10, M=7e5+50, Q=3e5+10, K=20;
int ans[Q],q,nid[Q][2],f[M][K+1];
struct solver{
string name;
int n,coe,tot,dep[M];
bool diag;
vector<pair<int,int>> a[N];
vector<int> e[M];
void ins(int x,int y,int id){
a[x].emplace_back(y,id);
}
void link(int x,int y){
// cout<<x<<"~"<<y<<"\n";
e[x].emplace_back(y);
e[y].emplace_back(x);
}
void dfs(int rt,int fa){
dep[rt]=dep[fa]+1;
f[rt][0]=fa;
rep(i,1,K) f[rt][i]=f[f[rt][i-1]][i-1];
for(int i:e[rt]) if(i!=fa){
dfs(i,rt);
}
}
int dist(int x,int y){
int xx,yy,res=0;
if(dep[x]<dep[y]) swap(x,y);
per(i,K,0){
xx=f[x][i];
if(dep[xx]>=dep[y]){
x=xx;
res+=(1<<i);
}
}
if(x!=y){
per(i,K,0){
xx=f[x][i];
yy=f[y][i];
if(xx!=yy){
x=xx;
y=yy;
res+=(2<<i);
}
}
x=f[x][0];
res+=2;
}
return x?res:-1;
}
void work(){
// cout<<name<<"\n";
rep(i,0,n-1){
sort(all(a[i]));
bool fr=true;
for(auto&[x,y]:a[i]){
if(fr){
fr=false;
continue;
}
if(y!=0){
nid[y<0?-y:y][y<0?0:1]=tot+1;
y=-1;
}else{
tot++;
y=tot;
}
}
erase_if(a[i],[](pair<int,int> x){return x.sec==-1;});
// for(auto&[x,y]:a[i]){
// cout<<x<<"&"<<y<<" ";
// }
// cout<<"\n";
}
rep(i,1,n-1){
auto i1=next(a[i-1].begin());
auto i2=next(a[i].begin());
while(i1!=a[i-1].end() && i2!=a[i].end()){
if(i1->fir > prev(i1)->fir+1 && i2->fir > prev(i2)->fir+1){
if(min(i1->fir,i2->fir)-max(prev(i1)->fir,prev(i2)->fir)-1>=(!diag)){
link(i1->sec,i2->sec);
}
}
if(i1->fir < i2->fir){
i1++;
}else{
i2++;
}
}
}
rep(i,1,tot){
if(!dep[i]){
dfs(i,0);
}
}
rep(i,1,q){
ans[i]+=dist(nid[i][0],nid[i][1])*coe;
}
}
}A,B,C,D;
int n,m;
void ins(int x,int y,int id=0){
A.ins(x,y,id);
B.ins(y,x,id);
C.ins(x+y,y-x+n+1,id);
D.ins(x-y+n+1,x+y,id);
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);
cin>>n>>m>>q;
A.name="A",B.name="B",C.name="C",D.name="D";
A.n=B.n=n+2;
C.n=D.n=2*n+3;
A.coe=B.coe=2;
C.coe=D.coe=1;
A.diag=B.diag=true;
C.diag=D.diag=false;
rep(i,0,n){
ins(0,i);
ins(n+1,i+1);
ins(i+1,0);
ins(i,n+1);
}
rep(i,1,m){
int x,y;
cin>>x>>y;
ins(x,y);
}
rep(i,1,q){
rep(j,0,1){
int x,y;
cin>>x>>y;
ins(x,y,j?i:-i);
}
}
A.work();
B.work();
C.work();
D.work();
rep(i,1,q) cout<<(ans[i]>=0?ans[i]/2:-1)<<"\n";
return 0;}
```
::::