Problem Description:
Given the graph g= (v,e). Set P is a simple way for g (vertices do not intersect) collection. If each vertex in V happens to be on one of the lines of P, then p is a path overlay of G. The path in P can start at any of the top points of V, and the length is arbitrary, in particular, can be 0. The minimum path coverage for G is the path coverage with the least number of path bars in G. an effective algorithm is designed to find the minimum path coverage of a direction-free graph G.
Tips:
Set v={1,2, ..., n}, construct network g1= (V1,E1) as follows:
Each edge has a capacity of 1. The maximum flow of the network G1 (X0,Y0) is obtained.
Programming tasks:
For a given given direction-free graph G, programming to find a minimum path overlay of G.
Data input:
the input data is provided by the file Input.txt. The 1th line of the file has 2 positive integers n and M. N is the number of vertices of the given direction-free graph g, and M is the number of sides of G. The next M-line has 2 positive integers i and j for each row, indicating a forward edge (I,J).
result output:
The minimum path overlay is output to the file Output.txt at the end of the program run. Starting with line 1th, each line outputs a path. The last line of the file is the minimum number of paths.
Exercises
Minimum path overlay, map method: Add all points to the X-and Y-sets of the binary graph, and if I can connect to J, let I in the X-set be connected to a J in the Y-sets, and the weight value is INF. Then let S connect to the X-set with a weight of 1. Let the y set connect to T, and the weight value is 1. Run the network stream, the number of paths is the total number of points minus the maximum stream value.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7#include <queue>8#include <vector>9 using namespacestd;Ten Const intinf=1e9; One Const intmaxn= -, maxm=60000; AInlineintRead () { - intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} the while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } - intN,m,s,t,ans; + structedge{ - intto,next,rest; + }E[MAXM]; A intHEAD[MAXN]; at intEcnt=1; -InlinevoidAddedge (intXintYintR) { -E[++ecnt].to=y; E[ecnt].rest=r; E[ECNT].NEXT=HEAD[X]; head[x]=ecnt; -E[++ecnt].to=x; e[ecnt].rest=0; E[ecnt].next=head[y]; head[y]=ecnt; - } - intDIS[MAXN]; in - BOOLBFS () { tomemset (DIS,0,sizeof(DIS)); +dis[s]=1; - Staticqueue<int>Q; the while(!Q.empty ()) Q.pop (); * Q.push (S); $ while(!Q.empty ()) {Panax Notoginseng intx=Q.front (); Q.pop (); - for(intI=head[x];i;i=E[i].next) { the inty=e[i].to; + if(e[i].rest&&dis[y]==0){ Adis[y]=dis[x]+1; the Q.push (y); + } - } $ } $ if(Dis[t])return true; - return false; - } the - intTO[MAXN];Wuyi BOOLVIS[MAXN]; the intDFS (intXintflow) { - if(x==t)returnflow; Wu intnow=0, TMP; - for(intI=head[x];i;i=E[i].next) { About if(e[i].rest&&dis[e[i].to]==dis[x]+1){ $Tmp=dfs (E[i].to,min (flow-now,e[i].rest)); - if(TMP) { -to[x]=e[i].to; - if(e[i].to-n>0) vis[e[i].to-n]=true; A } +e[i].rest-=tmp; thee[i^1].rest+=tmp; -now+=tmp; $ if(Now==flow)returnNow ; the } the } the if(!now) dis[x]=0; the returnNow ; - } in intDinic () { the intans=0; the while(BFS ()) { Aboutans+=DFS (s,1e9); the } the returnans; the } + intMain () { -Freopen ("path3.in","R", stdin); theFreopen ("Path3.out","W", stdout);BayiN=read (); m=read (); thes=0; t=2*n+1; the for(intI=1, u,v;i<=m;i++){ -U=read (); v=read (); -Addedge (u,n+v,inf); the } the for(intI=1; i<=n;i++){ theAddedge (S,i,1); theAddedge (N+i,t,1); - } theans=n-dinic (); the for(intI=1; i<=n;i++){ the if(vis[i]==false){//without the side of the I, I as starting point94printf"%d", i); the inttmp=i; the while(To[tmp]) { theprintf"%d", to[tmp]-N);98tmp=to[tmp]-N; About } -printf"\ n");101 } 102 }103printf"%d\n", ANS);104 return 0; the}
[Network Flow 24 questions] minimum path coverage problem