传送门
首先bfs得出每个门到每个点的最短距离dis。
二分最短时间+最大流判定。
如何判定?
假设二分到mid的时间,对于每个空地I,s->I,1,对于每一个门I,I->t,mid;
在这两排点的中间需要再加一排点,是把每一个门拆成mid个得到的,每个表示某个人用了某些时间到达了这个门。那么显然,对于某一个人i,某个门j,如果dis[i][j] < mid,那么i->j_dis[i][j],1;
最后对于每一个门拆成的点i_j,i_j->I,mid-j+1,这一步表示在j这个时间到达了i这个门的人只有mid-j+1个人可以通过。
大概长这个样子:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int max_n=25;
const int max_N=1e5+5;
const int max_m=1e6+5;
const int max_e=max_m*2;
const int INF=2e9;
int sx[4]={0,0,1,-1}; int sy[4]={1,-1,0,0};
int n,m,cntdoor,cntblock,l,r,N,maxflow,ans,mid;
char s[max_n],a[max_n][max_n];
int dis[max_n*max_n][max_n*max_n],number[max_n][max_n];bool vis[max_n][max_n];
struct hq{int x,y,step;}door[max_n*max_n],block[max_n*max_n];
int tot,point[max_N],next[max_e],v[max_e],remain[max_e];
int deep[max_N],cur[max_N];
queue <hq> p;
queue <int> q;
inline void get_dis(int x,int y,int st){
while (!p.empty()) p.pop();
p.push((hq){x,y,0});
while (!p.empty()){
hq now=p.front(); p.pop();
for (int i=0;i<4;++i){
int nowx=now.x+sx[i],nowy=now.y+sy[i];
if (nowx>0&&nowx<=n&&nowy>0&&nowy<=m&&a[nowx][nowy]=='.'&&!vis[nowx][nowy]){
vis[nowx][nowy]=true;
int end=number[nowx][nowy];
dis[st][end]=now.step+1;
p.push((hq){nowx,nowy,now.step+1});
}
}
}
}
inline void addedge(int x,int y,int cap){
++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
inline bool bfs(int s,int t){
memset(deep,0x7f,sizeof(deep)); deep[s]=0;
for (int i=1;i<=N;++i) cur[i]=point[i];
while (!q.empty()) q.pop(); q.push(s);
while (!q.empty()){
int now=q.front(); q.pop();
for (int i=point[now];i!=-1;i=next[i])
if (deep[v[i]]>INF&&remain[i]){
deep[v[i]]=deep[now]+1;
q.push(v[i]);
}
}
return deep[t]<INF;
}
inline int dfs(int now,int t,int limit){
if (now==t||!limit) return limit;
int f,flow=0;
for (int i=cur[now];i!=-1;i=next[i]){
cur[now]=i;
if (deep[v[i]]==deep[now]+1&&(f=dfs(v[i],t,min(limit,remain[i])))){
flow+=f;
limit-=f;
remain[i]-=f;
remain[i^1]+=f;
if (!limit) break;
}
}
return flow;
}
inline void dinic(int s,int t){
while (bfs(s,t))
maxflow+=dfs(s,t,INF);
}
int main(){
scanf("%d%d\n",&n,&m);
for (int i=1;i<=n;++i){
scanf("%s",s);
for (int j=1;j<=m;++j){
a[i][j]=s[j-1];
if (a[i][j]=='D') door[++cntdoor].x=i,door[cntdoor].y=j,number[i][j]=cntdoor;
if (a[i][j]=='.') block[++cntblock].x=i,block[cntblock].y=j,number[i][j]=cntblock;
}
}
memset(dis,0x7f,sizeof(dis));
for (int i=1;i<=cntdoor;++i){
memset(vis,0,sizeof(vis));
get_dis(door[i].x,door[i].y,i);
}
l=0; r=400; ans=-1;
while (l<=r){
mid=(l+r)>>1;
N=1+cntblock+cntdoor*mid+cntdoor+1;
tot=-1; memset(point,-1,sizeof(point)); memset(next,-1,sizeof(next));
for (int i=1;i<=cntblock;++i){
addedge(1,1+i,1);
}
for (int i=1;i<=cntdoor;++i){
addedge(1+cntblock+cntdoor*mid+i,N,mid);
}
for (int i=1;i<=cntblock;++i)
for (int j=1;j<=cntdoor;++j)
if (dis[j][i]<=mid){
addedge(1+i,1+cntblock+mid*(j-1)+dis[j][i],1);
}
for (int i=1;i<=cntdoor;++i)
for (int j=1;j<=mid;++j){
int now1=(i-1)*mid+j;
int now2=i;
addedge(1+cntblock+now1,1+cntblock+cntdoor*mid+now2,mid-j+1);
}
maxflow=0,dinic(1,N);
if (maxflow==cntblock) ans=mid,r=mid-1;
else l=mid+1;
}
if (ans!=-1) printf("%d\n",ans);
else printf("impossible\n");
}