HDU 3036 Escape grid chart multi-person Escape network stream | bipartite matching graph creation technique

Source: Internet
Author: User

Question:

Each '.' has a girl, E is the exit, '.' is the open space, and 'X' is the wall.

Every second, every girl can take one step (Up, down, or down)

Only one person can go out at each exit every second

Map of n * m, time limit T

Ask all girls if they can escape within T seconds. If they can output the minimum value, they cannot output "impossible"

Ideas:

Obviously, it is feasible to answer the second question + determine the network flow.

Because each exit can only go out for one person per second, each exit is split by time, and T is split into T points in T seconds.


Network Flow Graph

1. The source node creates an edge with 1 traffic for each girl.

2. If it takes t seconds for a girl to Exit a, the split point of a side with 1 traffic to Exit a is t seconds.

3. All Split points at each exit are connected to an edge with 1 traffic.

4. Split points x seconds for each exit u, split points to the x + 1 second of u to connect an edge with a traffic of inf (it indicates that the person from x second will not be able to exit from u if x second, you can wait until x + 1 seconds in u)


Minute by minute t second (that is, the answer) in the second point to determine whether the maximum stream is equal to the number of people


Create a bipartite match:

Enumeration time (because the maximum time is 12*12)

Add the following edge to the source image:

If the girl can exit within I, she will split the edge at all exit times.

Then, the original match is extended, and the cumulative maximum number of matches is increased.

When the maximum number of matches = the number of people, it is the minimum time.


The data is relatively watery. We can use a non-mainstream graph creation card. It is estimated that there are only 30 groups of data.


Network Flow code:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; # define ll int # define N 20050 # define M 105000 # define inf 10737418 struct Edge {ll from, to, cap, nex;} edge [M * 4]; // note that this must be large enough. Otherwise, there will be reverse arc ll head [N], edgenum; void add (ll u, ll v, ll cap) {Edge E = {u, v, cap, head [u]}; edge [edgenum] = E; head [u] = edgenum ++; Edge E2 = {v, u, 0, head [v]}; edge [edgenum] = E2; head [v] = edgenum ++;} ll sign [N]; bool BFS (ll from, ll) {memset (sign,-1, sizeof (sign); sign [from] = 0; queue
      
        Q; q. push (from); while (! Q. empty () {int u = q. front (); q. pop (); for (ll I = head [u]; I! =-1; I = edge [I]. nex) {ll v = edge [I]. to; if (sign [v] =-1 & edge [I]. cap) {sign [v] = sign [u] + 1, q. push (v); if (sign [to]! =-1) return true ;}}return false;} ll Stack [N], top, cur [N]; ll dinic (ll from, ll) {ll ans = 0; while (BFS (from, to) {memcpy (cur, head, sizeof (head); ll u = from; top = 0; while (1) {if (u = to) {ll flow = inf, loc; // loc indicates the minimum cap edge in the Stack for (ll I = 0; I <top; I ++) if (flow> edge [Stack [I]. cap) {flow = edge [Stack [I]. cap; loc = I ;}for (ll I = 0; I <top; I ++) {edge [Stack [I]. c Ap-= flow; edge [Stack [I] ^ 1]. cap + = flow;} ans + = flow; top = loc; u = edge [Stack [top]. from;} for (ll I = cur [u]; I! =-1; cur [u] = I = edge [I]. nex) // cur [u] indicates the subscript if (edge [I] of the edge where the u is located. cap & (sign [u] + 1 = sign [edge [I]. to]) break; if (cur [u]! =-1) {Stack [top ++] = cur [u]; u = edge [cur [u]. to;} else {if (top = 0) break; sign [u] =-1; u = edge [Stack [-- top]. from ;}}return ans;} void init () {memset (head,-1, sizeof head); edgenum = 0;} char mp [15] [15]; int n, m, T; int Hash (int x, int y) {return x * m + y;} vector
       
         E, P; int dis [150] [150], step [4] [2] = {,-, 0,-1 }; bool vis [150] [150]; void bfs (int sx, int sy) {int start = Hash (sx, sy); memset (vis, 0, sizeof vis ); vis [sx] [sy] = 1; dis [start] [start] = 0; queue
        
          Qx, qy; while (! Qx. empty () qx. pop (); while (! Qy. empty () qy. pop (); qx. push (sx), qy. push (sy); while (! Qx. empty () {int x = qx. front (), y = qy. front (); qx. pop (); qy. pop (); for (int I = 0; I <4; I ++) {int dx = x + step [I] [0], dy = y + step [I] [1]; if (! (0 <= dx & dx
         
           > 1; if (OK (mid) ans = min (ans, mid), r = mid-1; else l = mid + 1;} if (T binary match code:
          

# Include
           
            
# Include
            # Include
             
              
# Include
              
               
# Include
               
                
# Include
                
                  # Include
                 
                   # Include
                  
                    # Include
                   
                     # Include
                    
                      # Include
                     
                       # Include using namespace std;/* '0. macros, Preprocessers, */# pragma comment (linker, "/STACK: 102400000,102400000") # ifdef _ WIN32typedef _ int64 int64; # define OD (_ TYPE) printf ("% I64d \ n", _ TYPE) # define ODC (_ CASE, _ TYPE) printf ("Case % d: % I64d \ n", _ CASE ++, _ TYPE); # elsetypedef long int64; # define OD (_ TYPE) printf ("% lld \ n", _ TYPE) # define ODC (_ CASE, _ TYPE) printf ("Case % d: % lld \ n", _ CASE ++, _ TYPE); # endif # define LSON l, mid, id <1 # define RSON mid + 1, r, id <1 | 1 # define MM (l + r)> 1/* '1. constants, */const double EPS = 1e-8; const double PI = acos (-1.0); const int INF = 0x3f3f3f3f;/* '2. coding Area, */const int N = 15; const int M = 205; const int dir [4] [2] = {-1, 0}, {0, 1 },{ 1, 0 },{ 0,-1 }}; struct STATE {int x, y, t; STATE () {} STATE (int x, int y, int t): x (x), y (y), t (t) {}}; int uN, vN; vector
                      
                        G [M]; int linker [M * M]; // u matching bool used [M * M] for each v; char mp [N] [N]; int dis [M] [M]; // shortest time for each girl to exit bool vis [N] [N]; // int r, c, T accessed or not; int gid, eid; map
                       
                         , Int> mpg, mpe; // The number of each vertex bool dfs (int u) {for (unsigned int I = 0; I <g [u]. size (); I ++) {int v = g [u] [I]; if (used [v]) continue; used [v] = true; if (linker [v] =-1 | dfs (linker [v]) {linker [v] = u; return true ;}} return false ;} int hungary () {int res = 0; memset (linker,-1, sizeof (linker); for (int u = 0; u <uN; u ++) {memset (used, 0, sizeof (used); if (dfs (u) + + res;} return res;} void bfs (int x, int y) {queue
                        
                          Q; int eid = mpe [make_pair (x, y)]; memset (vis, 0, sizeof (vis); vis [x] [y] = 1; q. push (STATE (x, y, 0); while (! Q. empty () {STATE now = q. front (); q. pop (); for (int I = 0; I <4; I ++) {int dx = now. x + dir [I] [0]; int dy = now. y + dir [I] [1]; if (dx> = 0 & dx <r & dy> = 0 & dy <c) {if (mp [dx] [dy]! = '. ') Continue; if (vis [dx] [dy]) continue; int gid = mpg [make_pair (dx, dy)]; q. push (STATE (dx, dy, now. t + 1); dis [gid] [eid] = now. t + 1; vis [dx] [dy] = 1 ;}}} bool build_and_run (int limit) {for (int I = 0; I <M; I ++) g [I]. clear (); uN = gid, vN = eid * limit; for (int I = 0; I <gid; I ++) {for (int j = 0; j <eid; j ++) {for (int k = dis [I] [j]; k <= limit; k ++) {g [I]. push_back (j * limit + k ); }}} Int ans = hungary (); if (ans> = gid) return true; return false;} void gao () {gid = eid = 0; mpg. clear (); mpe. clear (); memset (dis, 0x3f, sizeof (dis); for (int I = 0; I <r; I ++) {scanf ("% s ", mp [I]); for (int j = 0; j <c; j ++) {if (mp [I] [j] = 'E ') mpe [make_pair (I, j)] = eid ++; if (mp [I] [j] = '. ') mpg [make_pair (I, j)] = gid ++ ;}} for (int I = 0; I <r; I ++) {for (int j = 0; j <c; j ++) {If (mp [I] [j] = 'E') {bfs (I, j) ;}} int L = 1, R = 256, ans = INF; while (L <= R) {int mid = (L + R)> 1; if (build_and_run (mid) {ans = mid; R = mid-1 ;} else {L = mid + 1 ;}}if (ans = INF | ans> T) {printf ("impossible \ n ");} else {printf ("% d \ n", ans) ;}} int main () {while (~ Scanf ("% d", & r, & c, & T) {memset (g, 0, sizeof (g); gao ();} return 0 ;}
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
           



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.