HDU-2732 leapin 'lizards Max stream

Source: Internet
Author: User

The question is that there are some lizard in a maze, and you cannot escape any number of these lizard. The question is given with only one row number, a distance that can jump as far as possible, and the number of columns is uncertain (the question tells the number of columns less than or equal to 20), but the data must be a matrix. Each lizard has an initial position. The question ensures that there are some columns in these positions. Each time the lizard jumps from one position to another, a pillar will fall due to the reaction. Obviously, this question can be solved using network streams. How can we make a chart? First, we need to determine a greedy idea, that is, if a column can jump directly out of the maze, then we will connect this point to the sink point, instead of connecting the vertex to another vertex. For the points that cannot be jumped out but have columns, we will search for other columns Based on the Skip distance. If any other columns can be found, we will connect these two points, in addition, the capacity is controlled to the number of columns at the end of the arc node. It is precisely because an arc can only constrain one vertex, so we need to split points, and the traffic between points is the number of columns. The second matrix given by the question is actually used to determine the source point. Be careful when entering this question. It must comply with the English syntax ~~

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <cstdio> # include <algorithm> # include <queue> # define Re (x) ^ 1) # define CP (x) + 500) # define INF 0x3fffffffusing namespace STD; // All l points are regarded as the source point, the capacity of the Super source and the point is 1 // and all the points with columns that can be detached are regarded as the sink points, the capacity of the Super settlement point is the number of arc-ending columns // all the columns that can be connected are connected. The capacity is the number of arc-ending columns int n, m, mm, dis [1000], head [1000], idx; const int source = 980, sink = 981; char G [25] [25], s [25] [25]; struct edge {int V, Cap, next ;} E [20000]; void Init () {idx =-1; memset (Head, 0xff, sizeof (head);} inline int to (int x, int y) {return x * m + Y;} inline bool out (int x, int y) {int u = x + 1, D = N-X, L = Y + 1, R = m-y; int Dist = min (u, min (D, min (L, R); Return Dist <= mm;} inline bool judge (int x, int y) {If (x <0 | x> = n | Y <0 | Y> = m) {return false;} else if (! G [x] [Y]) {return false;} return true;} void insert (int A, int B, int c) {++ idx; E [idx]. V = B, E [idx]. CAP = C; E [idx]. next = head [a], head [a] = idx;} void build (int x, int y) {int XX, YY; If (G [x] [Y]) {insert (to (x, y), CP (to (x, y), g [x] [Y]); insert (CP (to (x, y, y), to (x, y), g [x] [Y]); If (Out (x, y) {insert (CP (to (x, y, y), sink, INF); insert (sink, CP (to (x, y), 0);} else {for (INT I =-mm; I <= mm; ++ I) {for (Int J =-(Mm-ABS (I); j <= (Mm-ABS (I); ++ J) {xx = x + I, YY = Y + J; If (Judge (XX, YY )&&! (X = XX & Y = YY) {insert (CP (to (x, y), to (xx, YY ), G [x] [Y]); insert (to (xx, YY), CP (to (x, y), 0 );}}}}}} bool spfa (int u) {queue <int> q; memset (DIS, 0xff, sizeof (DIS); DIS [u] = 0; q. push (U); While (! Q. Empty () {u = Q. Front (); q. Pop (); For (INT I = head [u]; I! =-1; I = E [I]. next) {If (DIS [E [I]. v] =-1 & E [I]. cap> 0) {dis [E [I]. v] = dis [u] + 1; q. push (E [I]. v) ;}} return dis [sink]! =-1;} int DFS (int u, int flow) {If (u = sink) {return flow;} int TF = 0, SF; for (INT I = head [u]; I! =-1; I = E [I]. next) {If (DIS [u] + 1 = dis [E [I]. v] & E [I]. cap> 0 & (Sf = DFS (E [I]. v, min (flow-TF, E [I]. CAP) {e [I]. cap-= SF, E [Re (I)]. cap + = SF; TF + = SF; If (tf = flow) {return flow ;}} if (! TF) {dis [u] =-1;} return TF;} int dinic () {int ans = 0; while (spfa (source )) {ans + = DFS (source, INF);} return ans;} int main () {int T, CA = 0, ans; scanf ("% d ", & T); While (t --) {Init (); ans = 0; scanf ("% d", & N, & mm ); // m is the longest step for (INT I = 0; I <n; ++ I) {scanf ("% s", G [I]);} M = strlen (G [0]); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <m; ++ J) {G [I] [J]-= '0'; build (I, j) ;}} (INT I = 0; I <n; ++ I) {scanf ("% s", s [I]); For (Int J = 0; j <m; ++ J) {If (s [I] [J] = 'l') {++ ans; insert (source, to (I, j), 1 ); insert (to (I, j), source, 0) ;}} ANS-= dinic (); printf ("case # % d:", ++ CA ); if (! Ans) {puts ("no lizard was left behind. ");} else if (ANS = 1) {printf (" % d lizard was left behind. \ n ", ANS);} else {printf (" % d lizards were left behind. \ n ", ANS) ;}} return 0 ;}

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.