# BZOJ 2127: happiness（最小割）

185人阅读 评论(0)

## 2127: happiness

Time Limit: 51 Sec  Memory Limit: 259 MB
Submit: 1108  Solved: 524
[Submit][Status][Discuss]

1 2
1 1
100 110
1
1000

## Sample Output

1210
【样例说明】

【数据规模】

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 400000 + 10;
const int MAXM = 400000 + 10;
const int INF = 0x7fffffff;
int read()
{
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') f *= -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
struct Edge
{int to, next, cap, flow;}edge[MAXM];
int tot, head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
void init(){tot = 0; memset(head, -1, sizeof(head));}
void addedge(int u, int v, int w, int rw = 0)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].next = head[u];
edge[tot].flow = 0; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = rw; edge[tot].next = head[v];
edge[tot].flow = 0; head[v] = tot++;
}
int sap(int start, int end, int N)
{
memset(gap, 0, sizeof(gap));
memset(dep, 0, sizeof(dep));
memcpy(cur, head, sizeof(head));
int u = start; pre[u] = -1; gap[0] = N;
int ans = 0;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start; ans += Min; continue;
}
bool flag = false;
int v;
for(int i=cur[u];i!=-1;i=edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u])
{
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag)
{u = v; continue;}
int Min = N;
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}

int n, m;
int c1[110][110], c2[110][110], c3[110][110], c4[110][110], c5[110][110], c6[110][110];
int cal(int x, int y)
{
return (x - 1) * m + y;
}
int main()
{
n = read(), m = read();
int sum = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
c1[i][j] = read();
sum += c1[i][j];
c1[i][j] <<= 1;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
c2[i][j] = read();
sum += c2[i][j];
c2[i][j] <<= 1;
}
}
init(); int s = 0, t = n * m + 1;
int x;
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
{
x = read(); sum += x;
c1[i][j] += x; c1[i+1][j] += x;
addedge(cal(i, j), cal(i + 1, j), x, x);
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
{
x = read(); sum += x;
c2[i][j] += x; c2[i+1][j] += x;
addedge(cal(i, j), cal(i + 1, j), x, x);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<m;j++)
{
x = read(); sum += x;
c1[i][j] += x; c1[i][j+1] += x;
addedge(cal(i, j), cal(i, j + 1), x, x);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<m;j++)
{
x = read(); sum += x;
c2[i][j] += x; c2[i][j+1] += x;
addedge(cal(i, j), cal(i, j + 1), x, x);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
addedge(s, cal(i, j), c1[i][j]);
addedge(cal(i, j), t, c2[i][j]);
}
}
int ans = sap(s, t, n * m + 2);
printf("%d\n", sum - (ans>>1));
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：98623次
• 积分：3636
• 等级：
• 排名：第8929名
• 原创：275篇
• 转载：8篇
• 译文：0篇
• 评论：8条
阅读排行
评论排行
最新评论