[国家集训队]happiness

嘟嘟嘟


就这么建。
———

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int maxe = 5e6 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, m, t, sum = 0;
struct Edge
{
  int nxt, to, cap, flow;
}e[maxe];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y, int w)
{
  e[++ecnt] = (Edge){head[x], y, w, 0};
  head[x] = ecnt;
  e[++ecnt] = (Edge){head[y], x, 0, 0};
  head[y] = ecnt;
}

In int N(int x, int y, int flg) {return (x - 1) * m + y + n * m * flg;}

int dis[maxn];
In bool bfs()
{
  Mem(dis, 0), dis[0] = 1;
  queue<int> q; q.push(0);
  while(!q.empty())
    {
      int now = q.front(); q.pop();
      for(int i = head[now], v; ~i; i = e[i].nxt)
	{
	  if(!dis[v = e[i].to] && e[i].cap > e[i].flow)
	    dis[v] = dis[now] + 1, q.push(v);
	}
    }
  return dis[t];
}
int cur[maxn];
In int dfs(int now, int res)
{
  if(now == t || res == 0) return res;
  int flow = 0, f;
  for(int& i = cur[now], v; ~i; i = e[i].nxt)
    {
      if(dis[v = e[i].to] == dis[now] + 1 && (f = dfs(v, min(res, e[i].cap - e[i].flow))) > 0)
	{
	  e[i].flow += f; e[i ^ 1].flow -= f;
	  flow += f; res -= f;
	  if(res == 0) break;
	}
    }
  return flow;
}

In int minCut()
{
  int flow = 0;
  while(bfs())
    {
      memcpy(cur, head, sizeof(head));
      flow += dfs(0, INF);
    }
  return flow;
}

int main()
{
  //freopen("1.in", "r", stdin);
  Mem(head, -1);
  n = read(), m = read(); t = n * m * 5 + 1;
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j <= m; ++j)
      {
	int x = read(); sum += x;
	addEdge(0, N(i, j, 0), x);
      }
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j <= m; ++j)
      {
	int x = read(); sum += x;
	addEdge(N(i, j, 0), t, x);
      }
  for(int i = 1; i < n; ++i)
    for(int j = 1; j <= m; ++j)
      {
	int x = read(); sum += x;
	addEdge(0, N(i, j, 1), x);
	addEdge(N(i, j, 1), N(i, j, 0), INF);
	addEdge(N(i, j, 1), N(i + 1, j, 0), INF);
      }
  for(int i = 1; i < n; ++i)
    for(int j = 1; j <= m; ++j)
      {
	int x = read(); sum += x;
	addEdge(N(i, j, 2), t, x);
	addEdge(N(i, j, 0), N(i, j, 2), INF);
	addEdge(N(i + 1, j, 0), N(i, j, 2), INF);
      }
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j < m; ++j)
      {
	int x = read(); sum += x;
	addEdge(0, N(i, j, 3), x);
	addEdge(N(i, j, 3), N(i, j, 0), INF);
	addEdge(N(i, j, 3), N(i, j + 1, 0), INF);
      }
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j < m; ++j)
      {
	int x = read(); sum += x;
	addEdge(N(i, j, 4), t, x);
	addEdge(N(i, j, 0), N(i, j, 4), INF);
	addEdge(N(i, j + 1, 0), N(i, j, 4), INF);
      }
  write(sum - minCut()), enter;
  return 0;
}
posted @ 2019-04-29 20:03  mrclr  阅读(179)  评论(0编辑  收藏  举报