Split the ' I ' into 11 sides, an out point, in point and adjacent ' W ' with a capacity of 1 edge, out point and adjacent ' n ' with a capacity of 1, all the ' W ' and source points with a capacity of 1 sides, all the ' N ' and the meeting point with a capacity of 1 side, indicating only one time. A stream of the network is over.
Wrote the greedy 4000b+, and then the egg
#include <bits/stdc++.h>using namespacestd;Const intINF =0x3fffffff;Const intMAXN =2142;#definePB push_backstructedge{int from, To,cap,flow;}; Vector<Edge>Edges;vector<int>G[MAXN];intS, T;voidAddedge (int from,intTo,intcap) {edges. PB (edge{ from, To,cap,0}); Edges. PB (Edge{to, from,0,0}); intm =edges.size (); g[ from]. PB (M-2); G[to]. PB (M-1);}BOOLVIS[MAXN];intD[MAXN],CUR[MAXN];BOOLBFs () {memset (Vis,0,sizeof(VIS)); Queue<int>Q; Q.push (S); D[s]=0; Vis[s]=true; while(Q.size ()) {intU =Q.front (); Q.pop (); for(inti =0; I < g[u].size (); i++) {Edge&e =Edges[g[u][i]]; if(!vis[e.to] && e.cap >E.flow) {Vis[e.to]=true; D[e.to]= D[u] +1; Q.push (e.to); } } } returnvis[t];}intDfsintUinta) { if(U = = T | | a = =0)returnA; intFlow =0, F; for(int&i = Cur[u]; I < g[u].size (); i++) {Edge&e =Edges[g[u][i]]; if(D[u] +1= = D[e.to] && (f = DFS (E.to,min (a,e.cap-e.flow))) >0) {E.flow+=F; Edges[g[u][i]^1].flow-=F; Flow+=F; A-=F; if(A = =0) Break; } } returnflow;}intMaxflow () {intFlow =0; while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S,inf); } returnflow;} Vector<pii>Vec;Const intN = -;CharS[n][n];intId[n][n];intDx[] = {1,0,-1,0};intDy[] = {0,-1,0,1};intMain () {//freopen ("In.txt", "R", stdin); intN,m; scanf"%d%d",&n,&m); inticnt =0, wcnt =0, ncnt =0; for(inti =1; I <= N; i++) {scanf ("%s", s[i]+1); for(intj =1; J <= M; J + +){ if(S[i][j] = ='I') {Vec. PB (MP (i,j)); ID[I][J]= icnt++; } if(S[i][j] = ='W') {Id[i][j]= wcnt++; } if(S[i][j] = ='N') {Id[i][j]= ncnt++; } } } intMW = icnt*2+5; intMN = MW + wcnt +5; S= MN +ncnt +4; T = MN +ncnt +8; for(inti =0; I < vec.size (); i++){ intx = vec[i].fi, y =vec[i].se; int inch=2*i+1, out=2*i+2; Addedge (inch, out,1); for(intK =0; K <4; k++){ intNX = X+dx[k], NY = y+Dy[k]; if(S[nx][ny] = ='W'){ intU = id[nx][ny]+MW; Addedge (U,inch,1); } if(S[nx][ny] = ='N'){ intv = id[nx][ny]+MN; Addedge ( outV1); } } } for(inti =1; I <= N; i++) for(intj =1; J <= M; J + +){ if(S[i][j] = ='W'){ intv = id[i][j]+MW; Addedge (S,v,1); } if(S[i][j] = ='N'){ intU = id[i][j]+MN; Addedge (U,t,1); } } intAns =Maxflow (); printf ("%d", ans); return 0;}
Codeforce Gym 100203I I WIN (Network stream)