Wang Xiaodong linear planning and network flow 24 Questions
Overwrite and print the minimum vertex path
The minimum path coverage of a directed acyclic graph includes two types (set G to A directed acyclic graph, and set S to a path set of G ):
(1) minimum vertex path overwrite: For all vertices in G, I appears in one path in S, and only appears in one path in S, calculates the minimum capacity of S;
(2) minimum edge path coverage: For all edge e in G, e appears in one path in S, and only appears in one path in S, calculates the minimum capacity of S;
(1) minimum point path coverage:
Create a new graph and split each vertex I in G into two vertices I 'and I' 'in the new graph. If G has an edge <I, j> edge <I ', J ''> In The New Graph. Obviously, the new graph is a bipartite graph, and the maximum matching value is obtained (n-the maximum matching value of the new graph) is the minimum point path overwrite value.
Time Complexity: O (Nm) (Hungary algorithm)
(2) minimum edge path coverage:
For each vertex I in the graph, set d [I] To (I's inbound-I's Outbound) and classify the vertices in the graph according to d [I: d [I] <0 is called the "less incoming, more outgoing" Point, and D [I]> 0 is called the "less incoming, more" point, d [I] = 0 is called a point with equal inbound and outbound values. There are:
The value covered by the shortest edge path in a directed acyclic graph equals the sum of the D values of all vertices in the graph with "less incoming and outgoing.
Proof:
In fact, you only need to prove:For a directed acyclic graph with at least one edge, there must be a path. Its starting point is the point of "less incoming and outgoing", and its ending point is the point of "fewer incoming and outgoing, all the intermediate points are "Same as inbound and outbound" points.(You only need to find and delete this path in the graph until the graph is boundless ).
First, because there is no ring in the graph, there must be a "less incoming, less outgoing, and more outgoing" point.
Then, let's assume that the successor of all the "less inbound, less outbound, and more" vertices in the graph (Note: The successor is different from the descendant's, the descendant of a vertex includes all the successors of This vertex, all the successors, and all the successors ......) In addition, all vertices with "less incoming and more outgoing" constitute a closed subgraph. In this closed subgraph, because all vertices are "less-than-out", the sum of the input degrees of the entire subgraph must be greater than the sum of the output degrees, this is impossible (the sum of the inbound degrees of all vertices in a directed graph must be the sum of the outbound degrees of all vertices). Therefore, you can obtain the following information:In the figure, there must be at least one "less inbound and more" point, which is not the successor of "less inbound and more outbound.
In this way, select a vertex I among those successor vertices that do not have "less incoming and outgoing" and use it as the path start point, select a vertex J in the Next Step instead of "less incoming and outgoing". If J is a vertex of "less incoming and more outgoing", the edge <I, j> is a path that meets the conditions and ends. If such J is a point of equal "inbound and outbound", the descendant of J is considered: the successor of J may be "less incoming and outgoing, however, the descendant of J must have at least one vertex. j 'is not "less incoming and more outgoing" (otherwise, all descendant of J will also constitute a closed subgraph of "less incoming and outgoing ), among these js', there must be a forward trend of point I ', which is "less inbound and more outbound". Therefore, we need to discard the original path and use I' as the starting point of the new path, j 'is the first intermediate point of the new path. If J's successor is incomplete, in this case, if there is "less incoming", the path has been found. If both are "equal incoming and outgoing", the path will be extended due to the absence of loops in the figure, and a valid path will be found.
It can be obtained from this. For any directed acyclic graph, such a path exists, which proves the theorem for finding the minimum edge path overwrite value at the beginning.
The time complexity of the minimum edge path overwrite value of a directed acyclic graph: O (m + n ).
# Include <cstdio> # include <cstring> using namespace STD; const int maxn = 450; const int INF = 1 <25; const int S = 0; int n, m; struct edge {int V, next, W;} edge [maxn * maxn]; int head [maxn], CNT; // For sapvoid addedge (int u, int V, int W) {edge [CNT]. V = V; edge [CNT]. W = W; edge [CNT]. next = head [u]; head [u] = CNT ++; edge [CNT]. V = u; edge [CNT]. W = 0; edge [CNT]. next = head [v]; head [v] = CNT ++;} int SAP (INT t) {int pre [maxn], cur [maxn]; Int dis [maxn], gap [maxn]; int flow = 0, Aug = inf, U; bool flag; For (INT I = 0; I <= T; ++ I) {cur [I] = head [I]; Gap [I] = dis [I] = 0;} gap [s] = t + 1; U = pre [s] = s; while (DIS [s] <= T) {flag = 0; For (Int & J = cur [u]; ~ J; j = edge [J]. next) {int v = edge [J]. v; If (edge [J]. w> 0 & dis [u] = dis [v] + 1) {flag = 1; if (edge [J]. W <Aug) Aug = edge [J]. w; Pre [v] = u; u = V; If (u = T) {flow + = Aug; while (u! = S) {u = pre [u]; edge [cur [u]. w-= Aug; edge [cur [u] ^ 1]. W + = Aug;} Aug = inf;} break;} If (FLAG) continue; int mindis = t + 1; for (Int J = head [u]; ~ J; j = edge [J]. next) {int v = edge [J]. v; If (edge [J]. w> 0 & dis [v] <mindis) {mindis = dis [v]; cur [u] = J ;}} if (-- gap [dis [u] = 0) break; Gap [dis [u] = mindis + 1] ++; u = pre [u];} return flow;} // bool vis [maxn]; int path [maxn], PCNT; // Save the path and record the path length void DFS (int u) {vis [u] = true; path [PCNT ++] = u; for (int p = head [u]; ~ P; P = edge [p]. Next) if (! Vis [edge [p]. v]) {If (edge [p]. v) {If (edge [p]. W = 0) DFS (edge [p]. v-N) ;}}void build_graph () {for (INT I = 1; I <= N; ++ I) {addedge (0, I, 1 ); addedge (I + N, 2 * n + 1, 1) ;}} void Init () {memset (Head,-1, sizeof (head); CNT = 0 ;} int main () {int U, V; freopen ("path5.in", "r", stdin); freopen ("out1.txt", "W", stdout); While (~ Scanf ("% d", & N, & M) {Init (); For (INT I = 0; I <m; ++ I) {scanf ("% d", & U, & V); addedge (u, v + N, 1) ;}build_graph (); int ans = SAP (2 * n + 1); memset (VIS, false, sizeof (VIS); For (INT I = 1; I <= N; ++ I) {PCNT = 0; If (vis [I]) continue; DFS (I); For (Int J = 0; j <PCNT; ++ J) printf ("% d % C", path [J], j = pcnt-1? '\ N': '');} printf (" % d \ n ", N-ans);} return 0 ;}
Joj 2730 stock (produced by Torry tangniu)
Given n k-length sequences, ask how many pictures are required to generate all sequences so that the sequences of each graph do not overlap.
When a graph is opened, an undirected edge is connected based on the relationship between the two points ).
The correct method of constructing the graph is to convert the undirected definition to a partial order and then to the loop, that is to say, when a sequence is completely greater than another sequence, it is connected to a directed edge. In this way, it is a standard Dag, and it is better to find the minimum path overwrite.
#include <cstdio>#include <cstring>const int maxn=210;const int inf=1<<25;const int s=0;int n,m;struct edge{ int v,next,w;}edge[maxn*maxn];int head[maxn],cnt;//for sapvoid addedge(int u, int v, int w){ edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++;}int sap(int t){int pre[maxn],cur[maxn];int dis[maxn],gap[maxn]; int flow=0 , aug=inf ,u; bool flag; for (int i=0 ; i<=t ; ++i) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=t+1; u=pre[s]=s; while (dis[s]<=t) { flag=0 ; for (int &j=cur[u] ; ~j ; j=edge[j].next) { int v=edge[j].v; if (edge[j].w>0 && dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug)aug=edge[j].w; pre[v]=u; u=v; if (u==t) { flow+=aug; while (u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if (flag)continue ; int mindis=t+1; for (int j=head[u]; ~j ; j=edge[j].next) { int v=edge[j].v; if (edge[j].w>0 && dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if(--gap[dis[u]]==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow;}int stock[maxn][30];int k;bool valid (int x, int y){ for (int i=0 ; i<k ; ++i) { if(stock[x][i]<=stock[y][i])return false ; } return true ;}void build_graph()//o(n*n*k){ memset (head , -1 , sizeof(head)); cnt=0; for (int i=0 ; i<n ; ++i) { addedge(0 , i+1 , 1); addedge(i+1+n , 2*n+1 , 1); for (int j=0 ; j<n ; ++j) { if(valid(i,j)) addedge(i+1 , j+1+n , 1); } }}int main (){ int u,v; int cas; scanf("%d",&cas); for (int I=1 ; I<=cas ; ++I) { scanf("%d%d",&n,&k); for (int i=0 ; i<n ; ++i) { for (int j=0 ; j<k ; ++j) { scanf("%d",*(stock+i)+j); } } build_graph(); printf("Case #%d: %d\n",I,n-sap(2*n+1)); } return 0;}