1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| #include <bits/stdc++.h> #include <bits/extc++.h> using namespace __gnu_pbds; using namespace std;
namespace Legendgod { namespace Read { #define Fread #ifdef Fread const int Siz = (1 << 21) + 5; char *iS, *iT, buf[Siz]; #define gc() ( iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iS == iT ? EOF : *iS ++) : *iS ++ ) #define getchar gc #endif template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); } #undef getchar }
using namespace Read; const int maxn = (2e3) * 5 + 10; const int inf = 0x3f3f3f3f; int n, m, S, T; struct Edge { int to, next; int w, c; }edg[maxn * 20]; int head[maxn], cnt(1); void add1(int u,int v,int w,int c) { edg[++ cnt] = (Edge) {v, head[u], w, c}, head[u] = cnt; }
void add(int u,int v,int w,int c) { add1(u, v, w, c), add1(v, u, 0, -c); }
int h[maxn], dis[maxn], pre[maxn];
bool vis[maxn];
void Spfa() { memset(h, 0x3f, 4 * (T + 2)); static queue<int> q; while(!q.empty()) q.pop(); h[S] = 0, vis[S] = 1; q.push(S); while(!q.empty()) { int u = q.front(); q.pop();
vis[u] = 0; for(int i = head[u];i;i = edg[i].next) if(edg[i].w) { int to = edg[i].to; if(h[to] > h[u] + edg[i].c) { h[to] = h[u] + edg[i].c; if(!vis[to]) vis[to] = 1, q.push(to); } } } }
struct Node { int id, dis; int operator < (const Node &z) const { return dis > z.dis; } };
int Dij() { memset(dis, 0x3f, 4 * (T + 2)); memset(vis, 0, sizeof(vis)); memset(pre, 0, 4 * (T + 2)); dis[S] = 0; static __gnu_pbds::priority_queue<Node> q; while(!q.empty()) q.pop(); q.push({S, 0}); while(!q.empty()) { int u = q.top().id; q.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = head[u];i;i = edg[i].next) { int to = edg[i].to; int w = edg[i].w, c = edg[i].c; if(w <= 0) continue; int tmp = dis[u] + h[u] - h[to] + c; if(dis[to] > tmp) { dis[to] = tmp; pre[to] = i; q.push({to, dis[to]}); } } }
return dis[T] < inf; }
pair<int, int> Solve() { int x(0), y(0);
Spfa();
while(Dij()) { int ansf(inf), ansc(0); for(int i = T; i != S; i = edg[pre[i] ^ 1].to) ansf = min(ansf, edg[pre[i]].w); for(int i = T; i != S; i = edg[pre[i] ^ 1].to) { edg[pre[i]].w -= ansf, edg[pre[i] ^ 1].w += ansf; ansc += ansf * edg[pre[i]].c; } x += ansf, y += ansc; for(int i = 1; i <= T; ++ i) if(dis[i] < inf) h[i] += dis[i]; } return {x, y}; }
int mf[4] = {3, 4, 1, 2}; int d[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; int Sm, sumk(0); int id(int x,int y,int c) { return (x - 1) * m + y + c * Sm; } int a[2005][2005];
void build(int x,int y) { int op = (x + y) & 1; if(op) { add(S, id(x, y, 0), inf, 0); for(int i = 0; i < 4; ++ i) { int nx = x + d[i][0], ny = y + d[i][1]; if(nx < 1 || nx > n || ny < 1 || ny > m) continue; add(id(x, y, i + 1), id(nx, ny, mf[i]), 1, 0); } } else add(id(x, y, 0), T, inf, 0); function<void(int,int,int,int)> vadd; vadd = [&](int u,int v,int w,int c) { if(op == 1) add(u, v, w, c); else add(v, u, w, c); }; int W = a[x][y], ssk(0); for(int i = 0; i < 4; ++ i) if((W >> i) & 1) { ++ sumk, vadd(id(x, y, 0), id(x, y, i + 1), 1, 0); ++ ssk;
} int opsi[4] = { 3, 4, 1, 2 }; if(ssk == 1) { int ps = 0; for(int i = 0; i < 4; ++ i) if((W >> i) & 1) { ps = i + 1; break; } for(int i = 1; i <= 4; ++ i) if(i != ps && i != opsi[ps - 1]) vadd(id(x, y, ps), id(x, y, i), 1, 1); vadd(id(x, y, ps), id(x, y, opsi[ps - 1]), 1, 2); } else if(ssk == 2) { for(int i = 0; i < 2; ++ i) if((W >> i) & 1) if((W >> (i + 2)) & 1) return ; for(int i = 0; i < 4; ++ i) if((W >> i) & 1) { vadd(id(x, y, i + 1), id(x, y, opsi[i]), 1, 1); } } else if(ssk == 3) { int ps(0), nps; for(int i = 0; i < 4; ++ i) if(!((W >> i) & 1)) { ps = i + 1; break; } nps = opsi[ps - 1]; for(int i = 0; i < 4; ++ i) if((i + 1 != ps) && (i + 1 != nps)) vadd(id(x, y, i + 1), id(x, y, ps), 1, 1); vadd(id(x, y, nps), id(x, y, ps), 1, 2); } }
signed main() {
int i, j; r1(n, m); Sm = n * m; S = Sm * 5 + 1, T = S + 1; for(i = 1; i <= n; ++ i) for(j = 1; j <= m; ++ j) { r1(a[i][j]); build(i, j); } auto tmp = Solve();
if(tmp.first * 2 == sumk) printf("%d\n", tmp.second); else puts("-1"); return 0; }
}
signed main() { return Legendgod::main(), 0; }
|