http://www.lydsy.com/JudgeOnline/problem.php?id=1189
It is not qaq at all ....
So the worship of the Orz
Two minutes of time .....
It is then converted into a judgment question: how to pass within a finite period of time.
Suppose there is currently T time available to pass ... Then each door can be up to a person by t ...
And then all the points that can reach the door are connected, with unlimited capacity ....
Then the source to each point feasible point to connect the edge. Volume 1..
Then each door is connected to the edge of the sink. Capacity is T..
Then you can judge ....
(at first, the BFS wrote wrong Ah Qaq.
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int n=415, Oo=~0u>>1;const int dx[4]={1,-1, 0, 0}, Dy[4]={0, 0,-1, 1};int ihead[n], cnt=1;struct dat {int Next, to, from, Cap;} e[n*n*2];void Add (int u, int v, int c) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].from=u; e[cnt].to=v; e[cnt].cap=c;e[ ++CNT].NEXT=IHEAD[V]; ihead[v]=cnt; E[cnt].from=v; E[cnt].to=u; E[cnt].cap=0;} int p[n], d[n], gap[n], cur[n];int isap (int s, int t, int N) {for1 (i, 0, N) p[i]=0, d[i]=0, gap[i]=0, cur[i]=ihead[i];gap[ 0]=n; int ret=0, F, U=s, I;while (d[s]<n) {for (i=cur[u]; i; i=e[i].next) if (E[i].cap && d[e[i].to]+1==d[u]) break;if ( i) {p[e[i].to]=cur[u]=i; U=e[i].to;if (u==t) {for (f=oo; u!=s; u=e[p[u]].from) f=min (f, E[p[u]].cap); for (u=t; u!=s; u=e[p [u]]. From) e[p[u]].cap-=f, E[p[u]^1].cap+=f;ret+=f;}} else {if (! ( --gap[d[u]]) break;d[u]=n; Cur[u]=ihead[u];for (i=ihead[u]; i; i=e[i].next) if (E[i].cap && d[e[i].to]+1<d[u]) d[u]=d[e[i].to]+1;++gap [D[u]];if (U!=s) U=e[p[u]].from;}} return ret;} int dis[n][23][23], N, tot, M, a[30][30], S, t;struct Q {int x, y;} q[n];inline int ID (int x, int y){return (x-1) *m+y;} void BFs (int x, int y, int d[23][23]) {int front=0, Tail=0;for1 (i, 1, N) for1 (J, 1, m) d[i][j]=oo;q[tail].x=x, q[tail].y=y ; ++tail;d[x][y]=0; Q Tp;while (front!=tail) {tp=q[front++];x=tp.x, Y=tp.y;rep (i, 4) {int fx=dx[i]+x, fy=dy[i]+y;if (fx<1 | | fy<1 | | FX&G T;n | | Fy>m | | A[fx][fy]!=1 | | D[fx][fy]!=oo) Continue;d[fx][fy]=d[x][y]+1;q[tail].x=fx; Q[tail].y=fy; ++tail;}} For1 (i, 1, n) {For1 (J, 1, m) printf ("%d", d[i][j]==oo?0:d[i][j]); Puts ("");} Puts ("");} void Build () {For1 (i, 1, N) for1 (J, 1, m) if (a[i][j]==2) BFS (i, J, Dis[id (I, J)]);} BOOL Check (int t) {For1 (I, 1, t) Ihead[i]=0;cnt=1;for1 (i, 1, N) for1 (J, 1, m) if (a[i][j]==2) {int Id=id (i, j); For1 (x, 1, n ) For1 (Y, 1, m) if (a[x][y]==1 && dis[id][x][y]<=t) Add (ID (x, y), id, oo), add (ID, T, t);} For1 (i, 1, N) for1 (J, 1, m) if (a[i][j]==1) Add (S, ID (i, J), 1); return Isap (S, T, T) ==tot;} int main () {read (n); read (m); S=n*m+1, T=s+1;for1 (i, 1, N) for1 (J, 1, m) {char c=getchar (); while (c!= ' X ' &&c!=‘.‘ &&c!= ' D ') C=getchar (); if (c== '. ') a[i][j]=1, ++tot; if (c== ' D ') a[i][j]=2; }build (); int l=0, r=n*m+1, Mid;while (l<=r) {mid= (l+r) >>1;if (check (mid)) R=mid-1;else l=mid+1;} if (!check (r+1)) puts ("impossible"), Else printf ("%d\n", r+1); return 0;}
Description
There's been a fire, and all personnel need emergency evacuation! Suppose each room is a rectangular area of n m. Each lattice, if it is '. ', indicates that this is an empty space; if it is ' X ', then it is a wall, and if it is ' D ', then it is a door where people can withdraw from the room. It is known that the door must be on the boundary of the room and there will be no open space on the border. At first, there was a man in every open space, and every second in the evacuation, everyone could move a grid up and down in four directions, but he could still stand still. After the evacuation began, there was no limit on the number of vacant spaces (i.e. each vacant space could stand at the same time for countless people). However, because the door is very narrow, only one person can move to the door every second, once moved to the door position, it means that he has been safely evacuated. The question now is: If you want all people to evacuate safely, how long will it take? Or it's impossible to tell at all.
Input
The first line of the input file is a pair of positive integers separated by spaces N and M,3<=n <=20,3<=m<=20, and the following N-row M-column describes a matrix of n m. The elements can be characters '. ', ' X ' and ' D ', with no spaces between the characters.
Output
There is only one integer k, which indicates the shortest time for everyone to evacuate safely, and if it is impossible to evacuate, output ' impossible ' (not including quotes).
Sample INPUT5 5
Xxxxx
X... D
Xx. Xx
X.. Xx
Xxdxxsample Output3hint Source
"Bzoj" 1189: [HNOI2007] Emergency evacuation evacuate (two points +bfs+ network flow)