This blog post for the migration, written on May 16, 2015, does not represent my current views and views. Original address: http://blog.sina.com.cn/s/blog_6022c4720102w0d8.html
1. Prefacefirst, a concept-the minimum cut. What is the minimum cut? On the network stream, the smallest stream is truncated so that no traffic can flow from the source point to the sink point. Think about it, in fact, the biggest traffic flow. So we can get a theorem-
minimum cut = maximum flow . today's model is to convert the floor plan to a dual graph, using the shortest path to find the smallest cut. It is faster than bare minimum cut (dinic) (but only for grid plots). First look at an example:
Wolf Catch rabbit [Bzoj 1001/beijing 2006]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=3,m=4). There are three types of road 1: (x, y) <==> (x+1,y) 2: (x, y) <==> (x,y+1) 3: (x, y) <==> (x+1,y+1) The weights on the road indicate the maximum number of rabbits that can be passed on this route, 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 Formatthe first act n,m. Represents the size of the mesh, n,m are less than or equal to 1000. The first part of the next three parts total N rows, the number of M-1 per line, representing the weight of the horizontal road. The second part N-1 the line, the number of M per line, the weight of the longitudinal road. The third part N-1 the line, Represents the weighted value of a diagonal road. Input file guaranteed not to exceed 10M
output Formatoutputs an integer that represents the minimum number of wolves involved in an ambush.
Input Sample3 45 6 44 3 17 5 35 6 7 88 7 6 55 5 56 6 6
Output Sample -
2. ConversionIn fact, this problem, directly with dinic run maximum flow, is not WA, but the big data let dinic helpless. We notice that when we cut off a side (that is, send a wolf) draw a bar on the edge, and the smallest cut is to draw a bar rid with minimal cost. We
can think of our drawing of the bar as a link to the side of the block (if in the grid diagram) of the side, and you will find that to make the starting point and the end point is not connected, we will draw a string can be connected to the bar (can try), so this method of the minimum cut on the floor plan came into being. so the essence of this method is to run the shortest path on this string of secant. So, we're going to build a dual graph at this time: for each edge, there must be two faces on its left and right side. Then we will attach a side to the left and right two faces, and its weight is the weight of the original edge. That is, for a weight of 5 in the edge, in the dual graph corresponds to a 1 to 2 of the weight of the edge of 5. There's a problem at this point: What about the side? At this point, we set the lower-left section to a super-source point, and the upper-right section to a super-sink point. In this way, the dual graph is very well understood: we have to run from the super source to the Super meeting point the shortest way!
Code : (P.S. My code constructs a dual graph in a way that compares ghosts, though it's right. People can do their own brain repair better method = =)-----------------------------------------------------------------------------------------------------#include <cstdio>#include <cstring>#include <queue>#include <cstdlib>#define MAXN 1000005#define INF 0x3f3f3f3fusing namespace std; struct Edge{int v,next,val;};Edge EDGE[6*MAXN]; int ggd=inf;int cycle,n,m,x,now,h[maxn*6],s,t,dist[maxn*6],vis[maxn*6]; void Addedge (int u,int v,int val){now++;Edge[now].v=v;Edge[now].next=h[u];Edge[now].val=val;H[u]=now;} void Init1 (){for (int i=1;i<=n;i++)for (int j=1;j<=m-1;j++) {scanf ("%d", &x), Ggd=min (ggd,x);if (i==1) {Addedge (T, (i-1) *cycle+j*2,x); Addedge ((i-1) *cycle+j*2,t,x);}else if (i==n) {Addedge ((i-2) *cycle+j*2-1,s,x); Addedge (S, (i-2) *cycle+j*2-1,x);}else {Addedge (i-2) *cycle+j*2-1, (i-1) *cycle+j*2,x); Addedge ((I-1) *cycle+j*2, (i-2) *cycle+j*2-1,x);} }} void Init2 (){for (int i=1;i<=n-1;i++)for (int j=1;j<=m;j++) {scanf ("%d", &x), Ggd=min (ggd,x);if (j==1) {Addedge (S, (i-1) *cycle+1,x); Addedge ((i-1) *cycle+1,s,x);}else if (j==m) {Addedge (i*cycle,t,x); Addedge (t,i*cycle,x);}else {Addedge (i-1) *cycle+j*2-2, (i-1) *cycle+j*2-1,x); Addedge ((I-1) *cycle+j*2-1, (i-1) *cycle+j*2-2,x); } } } void Init3 (){int tot=-1;for (int i=1;i<=n-1;i++)for (int j=1;j<=m-1;j++) {scanf ("%d", &x); Tot+=2,ggd=min (ggd,x);Addedge (tot,tot+1,x); Addedge (tot+1,tot,x); }} void init (){scanf ("%d%d", &n,&m);cycle= (m-1) * *;s= (n-1) * (m-1) *2+1,t=s+1;init1 ();init2 ();init3 ();if (n==1| | M==1) {printf ("%d", GGD); exit (0); }} struct State{int num,nowval;State () {}State (int _num,int _nowval): num (_num), Nowval (_nowval) {}friend bool Operator < (state a,state b) {return a.nowval>b.nowval;}}; priority_queue q; int Dijkstra (){memset (dist,inf,sizeof (Dist));Q.push (State (s,0));dist[s]=0,vis[s]=0;While (Q.size ()) {State Temp=q.top (); Q.pop ();if (Temp.nowval>dist[temp.num]) continue;if (temp.num==t) return dist[t];for (int x=h[temp.num];x!=0;x=edge[x].next) {if (Dist[temp.num]+edge[x].val {Dist[edge[x].v]=dist[temp.num]+edge[x].val;Q.push (State (EDGE[X].V,DIST[EDGE[X].V)); } } }return-1;} int main (){init ();printf ("%d", Dijkstra ());return 0;}----------------------------------------------------------------------------------------------------------- --------UPDATE: Thanks to Wang Chen (@wyh2000) 1 hours to pay the debug to find the error ... [so the GGD variable is the one he's messing with, everybody ignore]
[Knowledge Point] the transformation of network stream dual graph