# 2127: happiness

Time Limit: 51 Sec Memory Limit: 259 MB
Submit: 2700 Solved: 1299
[Submit][Status][Discuss]

1 2

1 1

100 110

1

1000

1210

# 题解

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
inline long long max(long long a, long long b){return a > b ? a : b;}
inline long long min(long long a, long long b){return a < b ? a : b;}
inline long long abs(long long x){return x < 0 ? -x : x;}
inline void swap(long long &x, long long &y){long long tmp = x;x = y;y = tmp;}
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
struct Edge
{
long long u,v,w,nxt;
Edge(long long _u, long long _v, long long _w, long long _nxt){u = _u;v = _v;w = _w;nxt = _nxt;}
Edge(){}
}edge[1000010];
long long head[100010], cnt = 1, S, T, q[100010], he, ta, h[100010], ans;
inline void insert(long long a, long long b, long long c)
{
}
bool bfs()
{
memset(h, -1, sizeof(h)), h[S] = 0, he = ta = 0, q[ta ++] = S;
while(he < ta)
{
long long now = q[he ++];
for(long long pos = head[now];pos;pos = edge[pos].nxt)
{
long long v = edge[pos].v;
if(edge[pos].w && h[v] == -1)
h[v] = h[now] + 1, q[ta ++] = v;
}
}
return h[T] != -1;
}
long long dfs(long long x, long long f)
{
if(x == T) return f;
long long used = 0, w;
for(long long pos = head[x];pos;pos = edge[pos].nxt)
{
long long v = edge[pos].v;
if(h[v] == h[x] + 1)
{
w = dfs(v, min(edge[pos].w, f - used));
edge[pos].w -= w;
edge[pos ^ 1].w += w;
used += w;
if(used == f) return f;
}
}
if(!used) h[x] = -1;
return used;
}
void dinic()
{
while(bfs()) ans += dfs(S, INF);
}
long long n, m, sum, tot, wen[201][201], li[201][201], num[201][201],twenh[201][201],tlih[201][201],twenl[201][201],tlil[201][201];
int main()
{
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j <= m;++ j)
read(wen[i][j]), sum += wen[i][j], num[i][j] = ++ tot;
S = tot + 1, T = S + 1;
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j <= m;++ j)
for(long long i = 1;i < n;++ i)
for(long long j = 1;j <= m;++ j)
for(long long i = 1;i < n;++ i)
for(long long j = 1;j <= m;++ j)
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j < m;++ j)
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j < m;++ j)
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j <= m;++ j)
insert(S, num[i][j], wen[i][j] << 1), insert(num[i][j], T, li[i][j] << 1);
for(long long i = 1;i < n;++ i)
for(long long j = 1;j <= m;++ j)
insert(S, num[i][j], twenh[i][j]), insert(num[i][j], T, tlih[i][j]),
insert(S, num[i + 1][j], twenh[i][j]), insert(num[i + 1][j], T, tlih[i][j]),
insert(num[i][j], num[i + 1][j], twenh[i][j] + tlih[i][j]),
insert(num[i + 1][j], num[i][j], twenh[i][j] + tlih[i][j]);
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j < m;++ j)
insert(S, num[i][j], twenl[i][j]), insert(num[i][j], T, tlil[i][j]),
insert(S, num[i][j + 1], twenl[i][j]), insert(num[i][j + 1], T, tlil[i][j]),
insert(num[i][j], num[i][j + 1], twenl[i][j] + tlil[i][j]),
insert(num[i][j + 1], num[i][j], twenl[i][j] + tlil[i][j]);
dinic();
printf("%lld", sum - (ans >> 1));
return 0;
}

posted @ 2018-03-08 10:41  嘒彼小星  阅读(152)  评论(0编辑  收藏  举报