Hungarian algorithm Dfs
BOOLDfsintu) { for(inti =1; I <= N; i++){ if(A[u][i] &&!Visit[i]) {Visit[i]=true; if(Match[i] = =-1||DFS (Match[i])) {Match[i]=u; } return true; } } return false;}
2-sat
structtwosat{intN; Vector<int> g[maxn*2]; BOOLmark[maxn*2]; ints[maxn*2], C; BOOLDfsintx) { if(mark[x^1])return false; if(Mark[x])return true; MARK[X]=true; S[c++] =x; for(inti =0; I < g[x].size (); i++){ if(!dfs (G[x][i]))return false; } return true; } voidInitintN) { This->n =N; for(inti =0; I < n *2; i++) {g[i].clear (); } memset (Mark,0,sizeof(Mark)); } voidAdd_clause (intXintXval,intYintyval) {x= x *2+Xval; Y= y *2+Yval; G[x^1].push_back (y); G[y^1].push_back (x); } BOOLsolve () { for(inti =0; I < n *2; i + =2){ if(!mark[i] &&!mark[i +1]) {c=0; if(!DFS (i)) { while(C >0) {mark[s[--C]] =false; } if(!dfs (i +1)){ return false; } } } } return true; }};
Max-flow (Dinic)
structdinic{intN, M, I, S, t; Edge e; Vector<Edge>edges; Vector<int>G[MAXN]; intD[MAXN], CUR[MAXN]; BOOLVIS[MAXN]; voidInitintN) { This->n =N; for(i =0; I <= N; i++) {g[i].clear (); } edges.clear (); } voidAddedge (int from,intTo,intcap) {Edges.push_back (edge{ from, to, Cap,0 }); Edges.push_back (edge{to, from,0,0 }); M=edges.size (); g[ from].push_back (M-2); G[to].push_back (M-1); } BOOLBFS () {memset (Vis,0,sizeof(VIS)); Queue<int>Q; Q.push (s); D[s]=0; Vis[s]=1; while(!Q.empty ()) { intx =Q.front (); Q.pop (); for(i =0; I < g[x].size (); i++) {Edge& e =Edges[g[x][i]]; if(!vis[e.to] && e.cap >E.flow) {Vis[e.to]=true; D[e.to]= D[x] +1; Q.push (e.to); } } } returnVis[t]; } intDFS (intXinta) { if(x = = T | | a = =0)returnA; intFlow =0, F; for(int& i = cur[x]; I < g[x].size (); i++) {Edge& e =Edges[g[x][i]]; if(D[x] +1= = D[e.to] && (f = DFS (e.to, Min (A, e.cap-e.flow))) >0) {E.flow+=F; Edges[g[x][i]^1].flow-=F; Flow+=F; A-=F; if(A = =0) Break; } } returnflow; } intMaxflow (intSintTintneed) { intFlow =0; This->s =s; This->t =T; while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S, INF); if(Flow > Need)returnflow; } returnflow; } BOOLCheckfull (ints) { for(inti =0; I < g[s].size (); i++){ if(Edges[g[s][i]].flow! =Edges[g[s][i]].cap) { return false; } } return true; }};
Mcmf
structedge{intU,v,c,cost,next;} Edge[e];inthead[v],cnt;voidinit () {CNT=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintVintCintCost ) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].cost=Cost ; EDGE[CNT].C=c;edge[cnt].next=head[u];head[u]=cnt++; EDGE[CNT].U=v;edge[cnt].v=u;edge[cnt].cost=-Cost ; EDGE[CNT].C=0; edge[cnt].next=head[v];head[v]=cnt++;}BOOLSPFA (intBeginintend) { intu,v; Queue<int>Q; for(intI=0; i<=end+2; i++) {Pre[i]=-1; Vis[i]=0; Dist[i]=inf; } Vis[begin]=1; Dist[begin]=0; Q.push (begin); while(!Q.empty ()) {u=Q.front (); Q.pop (); Vis[u]=0; for(inti=head[u];i!=-1; i=Edge[i].next) { if(edge[i].c>0) {v=edge[i].v; if(dist[v]>dist[u]+edge[i].cost) {Dist[v]=dist[u]+Edge[i].cost; PRE[V]=i; if(!Vis[v]) {Vis[v]=true; Q.push (v); } } } } } returndist[end]!=inf;}intMCMF (intBeginintend) { intans=0, Flow; intflow_sum=0; while(SPFA (begin,end)) {Flow=inf; for(inti=pre[end];i!=-1; i=pre[edge[i].u])if(edge[i].c<flow) Flow=edge[i].c; for(inti=pre[end];i!=-1; i=PRE[EDGE[I].U]) {EDGE[I].C-=flow; Edge[i^1].c+=flow; } ans+=Dist[end]; Flow_sum+=flow; } //cout << flow_sum << Endl; returnans;}
Summer camp-Two minutes, network flow, 2-sat