# BZOJ 2127: happiness（最小割）

## 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 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 gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
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));
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;
{
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()
{
int sum = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
sum += c1[i][j];
c1[i][j] <<= 1;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
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++)
{
}
}
int ans = sap(s, t, n * m + 2);
printf("%d\n", sum - (ans>>1));
return 0;
}


• 本文已收录于以下专栏：

## BZOJ 2127 happiness 最小割

• PoPoQQQ
• 2015年01月11日 09:47
• 1697

## [BZOJ]2127: happiness 最小割

happiness： Description 高一一班的座位表是个n*m的矩阵，经过一个学期的相处，每个同学和前后左右相邻的同学互相成为了好朋友。这学期要分文理科了，每个同学对于选择文科与...

## BZOJ 2127: happiness 最小割 二元组建图

2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MB Submit: 2185  Solved: 1066 [Submit][Status]...

## BZOJ2127 happiness-最小割

• Mys_C_K
• 2017年05月11日 13:30
• 102

## BZOJ 2127: happiness

2127: happinessTime Limit: 51 Sec Memory Limit: 259 MB Submit: 2349 Solved: 1137 Description 高一...

## bzoj-2127 happiness

举报原因： 您举报文章：BZOJ 2127: happiness（最小割） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)