The main idea: give a table, each table points to one of the four squares around, ask you can change some of the lattice points, ask all the squares in the circle minimum need to change how much.
Idea: All the squares are in the circle, because each lattice can only have one out side, so make sure that all the squares have one edge. Create a model of the cost flow, where all points to the meeting point are connected to the flow 1 of the cost 0 of the edge, indicating that you want to accept an incoming edge. S an edge of 1 charge 0 for all points, indicating an out edge. A lattice to the surrounding four lattice connecting edge, Flow 1, if the direction is in line with the current direction, then the cost of 0, otherwise cost 1. Then running the cost stream is the answer.
CODE:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define MAX 10010#define INF 0x3f3f3f3f#define S 0#define T (MAX-1) using namespace Std;const int dx[] = {0, 0,0,-1,1};const int dy[] = {0,-1,1,0,0};int g[256];struct mincostmaxflow{int head[max],total;int Next[MAX],aim[MAX], Cost[max],flow[max];int F[max],from[max],p[max];bool V[max]; Mincostmaxflow () {total = 1;} void Add (int x,int y,int f,int c) {next[++total] = head[x];aim[total] = Y;flow[total] = F;cost[total] = c;head[x] = total; }void Insert (int x,int y,int f,int c) {Add (x,y,f,c); ADD (y,x,0,-c);} BOOL Spfa () {static queue<int> Q;while (!q.empty ()) Q.pop () memset (f,0x3f,sizeof (f)); Memset (V,false,sizeof (v)) ; F[s] = 0;q.push (S), while (!q.empty ()) {int x = Q.front (), Q.pop (); v[x] = false;for (int i = head[x]; i; i = Next[i]) if (flo W[i] && F[aim[i]] > F[x] + cost[i]) {F[aim[i]] = f[x] + cost[i];if (!v[aim[i]]) v[aim[i]] = True,q.push (aim[i]); f Rom[aim[i]] = X;p[aIm[i]] = i;}} return f[t]! = INF;} int Edmondskarp () {int re = 0;while (SPFA ()) {int max_flow = inf;for (int i = T; i = S; i = from[i]) max_flow = min (max_flow , Flow[p[i]]); for (int i = T; i = S; i = From[i]) {Flow[p[i]] = max_flow;flow[p[i]^1] + = Max_flow;} Re + = Max_flow * F[t];} return re;}} Solver;int m,n;int Num[20][20],cnt;char s[20][20];int Main () {g[' L '] = 1,g[' R '] = 2,g[' U '] = 3,g[' D '] = 4;cin >> m &G t;> n;for (int i = 1; I <= m; ++i) {scanf ("%s", S[i] + 1), for (int j = 1; j <= N; ++j) {num[i][j] = ++cnt;solver. Insert (s,cnt << 1,1,0); Solver. Insert (CNT << 1|1,t,1,0);}} for (int i = 1, i <= m; ++i) for (int j = 1; j <= N; ++j) for (int k = 1; k <= 4; ++k) {int FX = i + dx[k],fy = j + dy [K];if (!FX) FX = m;if (!fy) fy = n;if (FX > m) FX = 1;if (FY > N) fy = 1;if (k = = G[s[i][j]]) solver. Insert (Num[i][j] << 1,num[fx][fy] << 1|1,1,0); elsesolver. Insert (Num[i][j] << 1,num[fx][fy] << 1|1,1,1);} cout << Solver. Edmondskarp () << Endl;retuRN 0;}
Bzoj 3171 Tjoi 2013 cycle grid fee flow