[BZOJ1001] [Beijing2006] Wolf catch rabbit (Shortest way)

Source: Internet
Author: User

Description

Now the children's favorite "pleasant goat and ash too wolf", saying that the gray wolf catch the sheep, but the rabbit is still more in line, and now the rabbit is more stupid, they only two nest, now you as the Wolf King, facing the following a grid of terrain:

The upper-left corner is (n,m) and the lower-right corner is (n=4,m=5). There are three types of roads 1: (x, y) <==> (x+1,y) 2: (x, y) <==> (x,y+1) 3: (x, y) <==> (x+ 1,Y+1) The weight on the road indicates the maximum number of rabbits that can be passed on this road, and the road is non-direction. The upper-left and lower-right corners of the rabbit's two nests, at the beginning of all the rabbits gathered in the upper left corner of the nest, now they have to go to the lower right solution (n,m) in the nest, The Wolf King began to ambush the rabbits. Of course, for the sake of insurance, if a road up to the number of rabbits passed K, the Wolf King needs to arrange the same number of K wolf, in order to completely block the road, you need to help the Wolf King to arrange an ambush plan, so that the rabbit clean sweep under the premise of the number of wolves involved in the Because the wolf also to find pleasant sheep and sheep trouble.

Input

The first act N,m. Represents the size of the mesh, n,m are less than or equal to 1000. The next three parts of the first part of a total of N rows, the number of M-1 per row, representing the weight of the horizontal road. The second part of the total N-1 line, the number of M per row, indicating the weight of the longitudinal road. The third part of the total N-1 line, the number of M-1 per row, indicating the weighted value of the oblique road. Input file guaranteed not to exceed 10M

Output

Outputs an integer that represents the minimum number of wolves involved in an ambush.

Sample Input3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6 Sample Output -HINT

In 2015.4.16, a new group of data may be stuck in a previously available program.

SolutionAt first glance is a network flow of the problem, but the actual completion will find the timeout over to Grandma's home. Theorem: The maximum flow of the plane graph = the shortest circuit of the graph's dual graph, an image explanation see a thoughtful god Ben after SPFA disorderly get Dijkstra, but I built side of the way a bit of ghost animal.
1#include <cstdio>2#include <cstring>3 using namespacestd;4 structnode5 {6     intV, W;7}edge[6000005];8 intfst[2000005], nxt[6000005], SSS, TTT, dis[2000005], q[2000005], front, back;9 BOOLinq[2000005];Ten   One voidQSCANF (int&x) A { -     Charc =GetChar (); -x =0; the      while(C <'0'|| C >'9') -c =GetChar (); -      while(c >='0'&& C <='9') -x = x *Ten+ C-'0', C =GetChar (); + } -   + voidAddedge (intIintUintVintW) A { atEdge[i <<1] = (node) {V, w}, Nxt[i <<1] = Fst[u], fst[u] = i <<1; -Edge[i <<1|1] = (node) {u, w}, Nxt[i <<1|1] = Fst[v], fst[v] = i <<1|1; - } -   - voidSPFA () - { inmemset (DIS, the,sizeof(DIS)); -DIS[SSS] =0, inq[sss] = q[++back] =SSS; to      while(Front! =Back ) +     { -         intU = q[++front%2000000]; theInq[u] =false, Front%=2000000; *          for(inti = Fst[u]; I i =Nxt[i]) $         {Panax Notoginseng             intv = edge[i].v, w =EDGE[I].W; -             if(Dis[v] > Dis[u] +W) the             { +DIS[V] = Dis[u] +W; A                 if(!Inq[v]) the                 { +DIS[V] = Dis[u] +W; -INQ[V] =true; $Q[++back%2000000] =v; $Back%=2000000; -                 } -             } the         } -     }Wuyi } the   - intMain () Wu { -     intN, m, Etot =0; About qscanf (n), qscanf (m); $N--, m--; -     if(N &&m) -     { -SSS = n * m *2+1, TTT = n * M *2+2; A          for(inti =1; I <= n +1; i++) +              for(intj =1; J <= M; J + +) the             { -                 intU = ((i-2) * m + j) *2-1, V = ((i-1) * m + j) *2, W; $ qscanf (w); the                 if(i = =1) theAddedge (+ +)Etot, SSS, V, W); the                 Else if(i = = n +1) theAddedge (+ +)etot, U, TTT, W); -                 Else inAddedge (+ +)etot, U, V, W); the             } the          for(inti =1; I <= N; i++) About              for(intj =1; J <= m +1; J + +) the             { the                 intU = ((i-1) * m + j) *2-2, V = ((i-1) * m + j) *2-1, W; the qscanf (w); +                 if(J = =1) -Addedge (+ +)Etot, TTT, V, W); the                 Else if(j = = m +1)BayiAddedge (+ +)etot, U, SSS, W); the                 Else theAddedge (+ +)etot, U, V, W); -             } -          for(inti =1; I <= N; i++) the              for(intj =1; J <= M; J + +) the             { the                 intU = ((i-1) * m + j) *2-1, V = ((i-1) * m + j) *2, W; theQSCANF (W), Addedge (+ +etot, U, V, W); -             } the SPFA (); the     } the     Else94     { the         if(m) then =m; theDIS[TTT] =2147483647;98          for(inti =1; I <= N; i++) About         { - qscanf (Fst[i]);101             if(DIS[TTT] >Fst[i])102DIS[TTT] =Fst[i];103         }104     } theprintf"%d\n", dis[ttt]);106     return 0;107}
View Code

[BZOJ1001] [Beijing2006] Wolf catch rabbit (Shortest way)

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.