求出所有人撤离的最短时间。由于每扇门只能通过一次,所以不能简单用bfs来搞。
显然答案是有单调性的,考虑二分,问题变成了判断时间x所有人能不能撤离。
考虑最大流。对于每扇门,每个时间通过的人数最多为1,所以将每扇门按时间x来拆成x个点。连边(time/i,1,t)来限制流量。
另外对于每个人m,如果能在时间t到达门d,那么连边(m,d/t,1)。再把源点和所有人连一条容量为1的边。
则可以通过判断最大流是否满流来得出所有人能不能撤离。
由于(n,m)<=20.所以可以很轻松的跑出答案。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... char ss[N][N];
struct Node{int vis[N][N];}node[N*N];
int pos, num, n, m, ps[][]={,,,-,,,-,};
queue<int>Q;
queue<PII>que;
struct Edge{int p, next, w;}edge[];
int head[], cnt=, s, t, vis[]; void add_edge(int u, int v, int w){
edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
edge[cnt].p=u; edge[cnt].w=; edge[cnt].next=head[v]; head[v]=cnt++;
}
int bfs(){
int i, v;
mem(vis,-);
vis[s]=; Q.push(s);
while (!Q.empty()) {
v=Q.front(); Q.pop();
for (i=head[v]; i; i=edge[i].next) {
if (edge[i].w> && vis[edge[i].p]==-) {
vis[edge[i].p]=vis[v] + ;
Q.push(edge[i].p);
}
}
}
return vis[t]!=-;
}
int dfs(int x, int low){
int i, a, temp=low;
if (x==t) return low;
for (i=head[x]; i; i=edge[i].next) {
if (edge[i].w> && vis[edge[i].p]==vis[x]+){
a=dfs(edge[i].p,min(edge[i].w,temp));
temp-=a; edge[i].w-=a; edge[i^].w += a;
if (temp==) break;
}
}
if (temp==low) vis[x]=-;
return low-temp;
}
void bulid(int x){
mem(head,); cnt=;
s=; t=num+pos*x+;
FOR(i,,num) add_edge(s,i,);
FOR(i,,pos*x) add_edge(i+num,t,);
int tmp=;
FO(i,,n) FO(j,,m) if (ss[i][j]=='.') {
++tmp;
FOR(k,,pos) if (node[k].vis[i][j]&&node[k].vis[i][j]<=x) {
FOR(l,node[k].vis[i][j],x) add_edge(tmp,(k-)*x+l+num,);
}
}
}
bool check(int x){
bulid(x);
int sum=, tmp=;
while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp;
return sum==num;
}
int main ()
{
scanf("%d%d",&n,&m);
FO(i,,n) scanf("%s",ss[i]);
FO(i,,n) FO(j,,m) if (ss[i][j]=='.') ++num;
FO(i,,n) FO(j,,m) if (ss[i][j]=='D') {
++pos;
que.push(mp(i,j));
while (!que.empty()) {
PII tmp=que.front();
int x=tmp.first, y=tmp.second; que.pop();
FO(k,,) {
int dx=x+ps[k][], dy=y+ps[k][];
if (dx<||dx>=n||dy<||dy>=m||ss[dx][dy]!='.'||node[pos].vis[dx][dy]) continue;
que.push(mp(dx,dy)); node[pos].vis[dx][dy]=node[pos].vis[x][y]+;
}
}
}
int l=, r=, mid, flag=false;
while (l<r) {
mid=(l+r)>>;
if (check(mid)) r=mid, flag=true;
else l=mid+;
}
if (flag) printf("%d\n",r);
else puts("impossible");
return ;
}