bzoj2127 分类: bzoj templates 2015-08-07 16:33 14人阅读 评论(0) 收藏
最小割,差异代价模型。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
const int maxn = 105, maxm = 105;
const int noden = 10005, size = 4e5+5;
const int Nya = -1, INF = 0x3f3f3f3f;
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
int n, m, num[maxn][maxm], ans;
int W[maxn][maxm], L[maxn][maxm];
int WR[maxn][maxm], WC[maxn][maxm];
int LR[maxn][maxm], LC[maxn][maxm];
int WT[maxn][maxm], LT[maxn][maxm];
void init()
{
std::cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
read(W[i][j]), ans += W[i][j];
WT[i][j] += W[i][j]<<1;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
read(L[i][j]), ans += L[i][j];
LT[i][j] += L[i][j]<<1;
}
for(int i = 1; i < n; i++)
for(int j = 1; j <= m; j++)
{
read(WR[i][j]), ans += WR[i][j];
WT[i][j] += WR[i][j], WT[i+1][j] += WR[i][j];
}
for(int i = 1; i < n; i++)
for(int j = 1; j <= m; j++)
{
read(LR[i][j]), ans += LR[i][j];
LT[i][j] += LR[i][j], LT[i+1][j] += LR[i][j];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j < m; j++)
{
read(WC[i][j]), ans += WC[i][j];
WT[i][j] += WC[i][j], WT[i][j+1] += WC[i][j];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j < m; j++)
{
read(LC[i][j]), ans += LC[i][j];
LT[i][j] += LC[i][j], LT[i][j+1] += LC[i][j];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
num[i][j] = (i-1)*m+j;
}
struct FlowEdge
{
int v, cap, next;
FlowEdge(int v=0,int cap=0,int next=0):v(v),cap(cap),next(next){}
}edge[size];
int head[noden], el, ind, S, T;
void NewEdge(int u,int v,int cap)
{
edge[el] = FlowEdge(v,cap,head[u]), head[u] = el++;
edge[el] = FlowEdge(u, 0,head[v]), head[v] = el++;
}
struct Dinic
{
int dep[noden], cur[noden];
std::queue<int>line;
void build()
{
ind = n*m, S = ++ind, T = ++ind;
for(int i = 1; i <= ind; i++) head[i] = Nya;
for(int i = 1; i < n; i++)
for(int j = 1; j <= m; j++)
{
NewEdge(num[i][j], num[i+1][j], WR[i][j] + LR[i][j]);
NewEdge(num[i+1][j], num[i][j], WR[i][j] + LR[i][j]);
}
for(int i = 1; i <= n; i++)
for(int j = 1; j < m; j++)
{
NewEdge(num[i][j], num[i][j+1], WC[i][j] + LC[i][j]);
NewEdge(num[i][j+1], num[i][j], WC[i][j] + LC[i][j]);
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
NewEdge(S, num[i][j], WT[i][j]);
NewEdge(num[i][j], T, LT[i][j]);
}
}
bool BFS()
{
for(int i = 1; i <= ind; i++) dep[i] = 0;
dep[S] = 1, line.push(S);
while(!line.empty())
{
int now = line.front(); line.pop();
for(int i = head[now], p; i != Nya ; i = edge[i].next)
if(edge[i].cap && !dep[p = edge[i].v])
dep[p] = dep[now] + 1, line.push(p);
}
if(dep[T])
{
for(int i = 1; i <= ind; i++)
cur[i] = head[i];
return true;
}
else
return false;
}
int DFS(int a,int flow)
{
if(a == T)return flow;
int ret = 0;
for(int i = cur[a], p; i != Nya; i = edge[i].next)
if(dep[p = edge[i].v] == dep[a] + 1 && edge[i].cap)
{
int ff = DFS(p, std::min(flow,edge[i].cap));
flow -= ff, edge[i].cap -= ff;
ret += ff, edge[i^1].cap += ff;
if(!flow)
{
cur[a] = i;
return ret;
}
}
cur[a] = Nya;
return ret;
}
int main()
{
int totflow = 0;
while(true)
{
if(BFS() == false) break;
totflow += DFS(S, INF);
}
return totflow;
}
}dinic = {0};
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj2127.in","r",stdin);
freopen("bzoj2127.out","w",stdout);
#endif
init(), dinic.build();
ans -= dinic.main()>>1;
std::cout << ans;
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。