BZOJ 1189 紧急疏散(二分+最大流)
求出所有人撤离的最短时间。由于每扇门只能通过一次,所以不能简单用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-9 # define MOD 1000000000 # define INF 1000000000 # 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<<1,l,mid # define rch p<<1|1,mid+1,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=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=25; //Code begin... char ss[N][N]; struct Node{int vis[N][N];}node[N*N]; int pos, num, n, m, ps[4][2]={0,1,0,-1,1,0,-1,0}; queue<int>Q; queue<PII>que; struct Edge{int p, next, w;}edge[3200005]; int head[500005], cnt=2, s, t, vis[500005]; 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=0; edge[cnt].next=head[v]; head[v]=cnt++; } int bfs(){ int i, v; mem(vis,-1); vis[s]=0; Q.push(s); while (!Q.empty()) { v=Q.front(); Q.pop(); for (i=head[v]; i; i=edge[i].next) { if (edge[i].w>0 && vis[edge[i].p]==-1) { vis[edge[i].p]=vis[v] + 1; Q.push(edge[i].p); } } } return vis[t]!=-1; } 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>0 && vis[edge[i].p]==vis[x]+1){ a=dfs(edge[i].p,min(edge[i].w,temp)); temp-=a; edge[i].w-=a; edge[i^1].w += a; if (temp==0) break; } } if (temp==low) vis[x]=-1; return low-temp; } void bulid(int x){ mem(head,0); cnt=2; s=0; t=num+pos*x+1; FOR(i,1,num) add_edge(s,i,1); FOR(i,1,pos*x) add_edge(i+num,t,1); int tmp=0; FO(i,0,n) FO(j,0,m) if (ss[i][j]=='.') { ++tmp; FOR(k,1,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-1)*x+l+num,1); } } } bool check(int x){ bulid(x); int sum=0, tmp=0; while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp; return sum==num; } int main () { scanf("%d%d",&n,&m); FO(i,0,n) scanf("%s",ss[i]); FO(i,0,n) FO(j,0,m) if (ss[i][j]=='.') ++num; FO(i,0,n) FO(j,0,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,0,4) { int dx=x+ps[k][0], dy=y+ps[k][1]; if (dx<0||dx>=n||dy<0||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]+1; } } } int l=0, r=405, mid, flag=false; while (l<r) { mid=(l+r)>>1; if (check(mid)) r=mid, flag=true; else l=mid+1; } if (flag) printf("%d\n",r); else puts("impossible"); return 0; }