## 【Codeforces 802C】Heidi and Library (hard)

### 解题报告

1. 上下界最小费用流：
直接按照上下界网络流一样建图，然后正常跑费用流
2. 带负环的费用流
应用消圈定理，强行将负环满流

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 5000000;
const int M = 200;
const int INF = 1e9;

int n,k,S,T,tot,SS,TT,ans,a[M],np[M],cc[M];

char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline int AddEdge(int u, int v, int c, int f) {
static int E = 1;
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f; cost[E] = c;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0; cost[E] = -c;
}

class Minimum_Cost_Flow{
int dis[N],sur[N],inq[N],vis[N];
queue<int> que;
public:
inline void MaxFlow() {
while (clearCircle());
for (int ff; ff = INF, SPFA();) {
for (int w = TT; w != SS; w = to[sur[w]^1]) {
ff = min(ff, flow[sur[w]]);
}
for (int w = TT; w != SS; w = to[sur[w]^1]) {
flow[sur[w]] -= ff;
flow[sur[w]^1] += ff;
}
ans += dis[TT] * ff;
}
}
private:
bool SPFA() {
memset(dis,60,sizeof(dis));
que.push(SS); dis[SS] = 0;

while (!que.empty()) {
int w = que.front(); que.pop(); inq[w] = 0;
if (dis[to[i]] > dis[w] + cost[i] && flow[i]) {
dis[to[i]] = dis[w] + cost[i];
sur[to[i]] = i;
if (!inq[to[i]]) {
inq[to[i]] = 1;
que.push(to[i]);
}
}
}
}
return dis[TT] < INF;
}
bool clearCircle() {
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= tot; ++i) {
if (!vis[i] && DFS(i)) {
return 1;
}
}
return 0;
}
bool DFS(int w) {
vis[w] = 1;
if (inq[w]) {
int cur = w;
do {
flow[sur[cur]]--;
flow[sur[cur]^1]++;
ans += cost[sur[cur]];
cur = to[sur[cur]];
} while (cur != w);
return 1;
} else {
inq[w] = 1;
for (int i = head[w]; i; i = nxt[i]) {
if (flow[i] && dis[to[i]] > dis[w] + cost[i]) {
dis[to[i]] = dis[w] + cost[i];
sur[w] = i;
if (DFS(to[i])) {
inq[w] = 0;
return 1;
}
}
}
inq[w] = 0;
return 0;
}

}
}MCMF;

int main() {
#ifdef DBG
freopen("11input.in", "r", stdin);
#endif
S = tot = n + 4; T = n + 1;
SS = n + 2; TT = n + 3;
for (int i = 1; i <= n; i++) {
np[i] = ++tot;
AddEdge(np[i], i + 1, 0, INF);
}
for (int i = 1; i <= n; i++) {
}
for (int i = 1; i <= n; i++) {
ans += cc[a[i]];
for (int j = i + 1; j <= n; j++) {
if (a[i] == a[j]) {
break;
}
}
}
MCMF.MaxFlow();
cout<<ans<<endl;
return 0;
}


## 【BZOJ 3577】玩手机

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int INF = 1e9;
const int N = 500000;
const int M = 2000000;

int S,T,E,tot,A,B,Y,X,n2[2][70][70][8];

char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline void AddEdge(int u, int v, int f) {
assert(u); assert(v);
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0;
}

class NetworkFlow{
int dis[N],cur[N];
queue<int> que;
public:
inline int MaxFlow() {
int ret = 0;
while (BFS()) {
ret += DFS(S, INF);
}
return ret;
}
private:
inline bool BFS() {
memset(dis, 60, sizeof(dis));
dis[S] = 0;
for (que.push(S); !que.empty(); que.pop()) {
int w = que.front();
for (int i = head[w]; i; i = nxt[i]) {
if (flow[i] && dis[to[i]] > INF) {
dis[to[i]] = dis[w] + 1;
que.push(to[i]);
}
}
}
return dis[T] <= INF;
}
inline int DFS(int w, int f) {
if (w == T) {
return f;
} else {
int ret = 0;
for (int &i = cur[w]; i; i = nxt[i]) {
if (flow[i] && dis[to[i]] == dis[w] + 1) {
int tmp = DFS(to[i], min(f, flow[i]));
ret += tmp; f -= tmp;
flow[i] -= tmp; flow[i ^ 1] += tmp;
if (!f) {
break;
}
}
}
return ret;
}
}
}Dinic;

int main() {
#ifdef DBG
freopen("11input.in", "r", stdin);
#endif
S = ++tot; T = ++tot;
E = 1;
for (int i = 1; i <= X; ++i) {
for (int j = 1; j <= Y; ++j) {
n1[0][i][j] = ++tot;
n1[1][i][j] = ++tot;
}
}
for (int i = X; i; --i) {
for (int j = Y; j; --j) {
for (int a = 0, len = 1; i + len - 1 <= X && j + len - 1 <= Y; ++a, len <<= 1) {
n2[0][i][j][a] = ++tot;
n2[1][i][j][a] = ++tot;
if (!a) {
} else {
int llen = len >> 1;
AddEdge(n2[0][i][j][a], n2[0][i + llen][j][a - 1], INF);
AddEdge(n2[0][i][j][a], n2[0][i][j + llen][a - 1], INF);
AddEdge(n2[0][i][j][a], n2[0][i + llen][j + llen][a - 1], INF);

AddEdge(n2[1][i][j + llen][a - 1], n2[1][i][j][a], INF);
AddEdge(n2[1][i + llen][j][a - 1], n2[1][i][j][a], INF);
AddEdge(n2[1][i + llen][j + llen][a - 1], n2[1][i][j][a], INF);
}
}
}
}
for (int i = 1, w, x1, x2, y1, y2, p0, p1; i <= A; ++i) {
p0 = ++tot; p1 = ++tot;

int len = x2 - x1 + 1, lg = 0, d = 1;
for (; (d << 1) <= len; lg++, d <<= 1);
AddEdge(p1, n2[0][x1][y2 - d + 1][lg], INF);
AddEdge(p1, n2[0][x2 - d + 1][y1][lg], INF);
AddEdge(p1, n2[0][x2 - d + 1][y2 - d + 1][lg], INF);
}
for (int i = 1, w, x1, x2, y1, y2, p0, p1; i <= B; ++i) {
p0 = ++tot;	p1 = ++tot;

int len = x2 - x1 + 1, lg = 0, d = 1;
for (; (d << 1) <= len; lg++, d <<= 1);
AddEdge(n2[1][x1][y2 - d + 1][lg], p0, INF);
AddEdge(n2[1][x2 - d + 1][y1][lg], p0, INF);
AddEdge(n2[1][x2 - d + 1][y2 - d + 1][lg], p0, INF);
}
assert(tot < N);
assert(E < M);
printf("%d\n", Dinic.MaxFlow());
return 0;
}


## 【CodeChef DINING】[January Cook-off 2014] Dining

### 解题报告

http://oi.cyo.ng/?p=2702

## 【HDU 5772】String problem

### 解题报告

1. 子序列看成子串
2. 值域范围$0 \sim 9$看漏

## 【BZOJ 1937】[SHOI2004] Mst最小生成树

### 解题报告

←为什么这个频率这个鬼畜啊 QwQ

## 【日常小测】cut

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 109;
const int M = N * N << 1;
const int INF = 1e9;

struct Data{
int u,v,val;
inline Data() {}
inline Data(int a, int b, int c):u(a),v(b),val(c) {}
inline bool operator < (const Data &B) const {
return val > B.val;
}
}p[N*N*N];

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline void AddEdge(int u, int v, int c) {
static int E = 1; cost[E+1] = cost[E+2] = c;
}

int cal(int w, int f, int pur, int mn) {
if (w == pur) return mn;
if (to[i] != f) {
tmp = cal(to[i], w, pur, min(mn, cost[i]));
if (~tmp) return tmp;
}
} return -1;
}

int find(int x) {return x==fa[x]? x: fa[x]=find(fa[x]);}

int main() {
for (int i=1,v;i<=n;i++) {
fa[i] = i;
for (int j=1;j<=n;j++) {
p[++tot] = Data(i, j, v);
}
}
sort(p+1, p+1+tot);
for (int i=1;i<=tot;i++) {
int u=p[i].u, v=p[i].v, val=p[i].val;
if (find(u) == find(v)) {if(cal(u,u,v,INF)!=val)cout<<-1,exit(0);}
else fa[find(u)] = fa[v], AddEdge(u, v, val);
}
cout<<n-1<<endl;
for (int i=2;to[i];i+=2) printf("%d %d %d\n",to[i],to[i+1],cost[i]);
return 0;
}


## 【LA 5928】[2016-2017 ACM-ICPC CHINA-Final] Mr.Panda and TubeMaster

### 中文题面

Mr. Panda很喜欢玩游戏。最近，他沉迷在一款叫Tube Master的游戏中。

1. 每个格子要么没有管道，要么这个格子的管道是环形管道的一部分。
2. 每个关键格子必须放置管道。

Mr. Panda想要打赢这局游戏并且拿到尽可能多的分数。你能帮他计算他最多能拿多少分吗？

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 5000;
const int M = 100000;
const int INF = 1e9;

int pos[30][30],cx[30][30],cy[30][30],vis[30][30];

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

inline int AddEdge(int u, int v, int c, int f) {
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f; cost[E] = c;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0; cost[E] = -c;
return E - 1;
}

class Minimum_Cost_Flow{
int dis[N],sur[N],inq[N];
queue<int> que;
public:
inline int MaxFlow() {
int ret_cost = 0, ret_flow = 0;
for (int f=INF,w;SPFA();f=INF) {
for (w=T;w!=S;w=to[sur[w]^1]) f = min(f, flow[sur[w]]);
for (w=T;w!=S;w=to[sur[w]^1]) flow[sur[w]] -= f, flow[sur[w]^1] += f;
ret_cost += dis[T] * f;
ret_flow += f;
}
return ret_flow == n * m? ret_cost: -INF;
}
private:
bool SPFA() {
memset(dis,60,sizeof(dis));
que.push(S); dis[S] = 0;

while (!que.empty()) {
int w = que.front(); que.pop(); inq[w] = 0;
if (dis[to[i]] > dis[w] + cost[i] && flow[i]) {
dis[to[i]] = dis[w] + cost[i];
sur[to[i]] = i;
if (!inq[to[i]]) inq[to[i]] = 1, que.push(to[i]);
}
}
}
return dis[T] < INF;
}
}MCMF;

inline int id(int x, int y, int t) {
return ((y - 1) * n + x - 1) * 2 + t;
}

int main() {
S = 0; T = N - 1; E = 1; memset(head,0,sizeof(head));
for (int j=1,v;j<=m;j++) for (int i=1;i<n;i++) cx[i][j] = -read();
for (int j=1,v;j<m;j++) for (int i=1;i<=n;i++) cy[i][j] = -read();
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
if (vis[i][j] != t) AddEdge(id(i,j,1), id(i,j,2), 0, 1);
}
}
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
if ((i + j) & 1) {
if (i < n) AddEdge(id(i+1,j,1), id(i,j,2), cx[i][j], 1);
if (i > 1) AddEdge(id(i-1,j,1), id(i,j,2), cx[i-1][j], 1);
if (j < m) AddEdge(id(i,j,1), id(i,j+1,2), cy[i][j], 1);
if (j > 1) AddEdge(id(i,j,1), id(i,j-1,2), cy[i][j-1], 1);
}
}
}
int tmp = MCMF.MaxFlow();
if (tmp == -INF) puts("Impossible");
else printf("%d\n",-tmp);
}
return 0;
}


—————————— UPD 2017.3.22 ——————————
Claris给我讲了一点新的姿势，不需要原来的无源汇带上下界费用流了

Claris真是太强了 _(:з」∠)_

## 【日常小测】摩尔庄园

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 100009;
const int INF = 1e9;

int  n,m,vout,cnt[N],pos[N];
int sur[N],val[N],cost[N];

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

inline int LCA(int u, int v) {
for (;u!=v;u>>=1)
if (u < v) swap(u, v);
return u;
}

inline void update(int w) {
static int ls, rs, cl, cr;
if (cnt[w]) sur[w] = w, val[w] = 0;
else sur[w] = 0, val[w] = INF;
if ((ls=w<<1) <= n) {
cl = cost[ls] > 0? -1: 1;
if(val[ls] + cl < val[w]) {
val[w] = val[ls] + cl;
sur[w] = sur[ls];
}
}
if ((rs=ls|1) <= n) {
int cr = cost[rs] > 0? -1: 1;
if(val[rs] + cr < val[w]) {
val[w] = val[rs] + cr;
sur[w] = sur[rs];
}
}
}

inline void modify(int w, int p, int delta) {
while (w != p) {
cost[w] += delta;
update(w); w >>= 1;
}
}

inline int query(int w, int &ans) {
static int ret, delta; delta = 0;
for (;w;w>>=1) {
if (val[w] + delta < ans) {
ans = val[w] + delta;
ret = sur[w];
}
delta += cost[w] >= 0? 1: -1;
} return ret;
}

int main() {
for (int i=1;i<=n;i++) cnt[i] = read();
for (int i=1;i<=m;i++) pos[i] = read();
for (int i=n;i;i--) update(i);
for (int i=1,u,v,ans=INF;i<=m;++i,ans=INF) {
cnt[v=query(u=pos[i], ans)]--;
printf("%d\n",vout+=ans);
int lca = LCA(u, v);
modify(u, lca, 1); modify(v, lca, -1);
for (;lca;lca>>=1) update(lca);
}
return 0;
}


## 【Yandex Contest 3529D】[NEERC 2016] Delight for a Cat

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 1009;
const int M = 5000;
const LL INF = 1e18;

int n,k,me,ms,S,T,s[N],e[N],edge[N];
LL vout,dis[N];

inline int Add_Edge(int u, int v, int f, int c) {
static int E = 1;
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f; cost[E] = c;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0; cost[E] = -c;
return E - 1;
}

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

class Minimum_Cost_Flow{
int sur[N],inq[N];
queue<int> que;
public:
inline LL MaxFlow() {
LL ret_cost = 0;
for (int f=1e9,w;SPFA();f=1e9) {
for (w=T;w!=S;w=to[sur[w]^1]) f = min(f, flow[sur[w]]);
for (w=T;w!=S;w=to[sur[w]^1]) flow[sur[w]] -= f, flow[sur[w]^1] += f;
ret_cost += dis[T] * f;
}
return ret_cost;
}
private:
bool SPFA() {
fill(dis, dis+N, INF);
que.push(S); dis[S] = 0;

while (!que.empty()) {
int w = que.front(); que.pop(); inq[w] = 0;
if (dis[to[i]] > dis[w] + cost[i] && flow[i]) {
dis[to[i]] = dis[w] + cost[i];
sur[to[i]] = i;
if (!inq[to[i]]) inq[to[i]] = 1, que.push(to[i]);
}
}
}
return dis[T] < INF;
}
}MCMF;

int main() {
S = 0; T = N - 1;
for (int i=1;i<=n;i++) vout += (s[i] = read());
for (int i=1;i<=n;i++) e[i] = read();
for (int i=2;i<=n-k+2;i++) Add_Edge(i, i-1, k-ms-me, 0);
for (int i=1;i<=n;i++) edge[i] = Add_Edge(min(i+1, n-k+2), max(1, i-k+1), 1, s[i] - e[i]);
printf("%lld\n",vout-MCMF.MaxFlow());
for (int i=1;i<=n;i++) putchar(flow[edge[i]]? 'S': 'E');
return 0;
}


## 【TopCoder SRM558】Surrounding Game

### 解题报告

$j$ 是点 $i$ 拆出来的点， $a$ 、 $b$ 、 $c$ 、 $d$ 是与i点相邻的点拆出来的点

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 1000;
const int M = 100000;
const int INF = 1e9;

int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1,0};

class Network_Flow{
int cur[N],dis[N];
queue<int> que;
public:
inline int MaxFlow() {
int ret = 0;
while (BFS()) {
ret += DFS(S, INF);
}
return ret;
}
private:
inline bool BFS() {
memset(dis,60,sizeof(dis));
que.push(S); dis[S] = 0;

while (!que.empty()) {
int w = que.front(); que.pop();
if (dis[to[i]] > INF && flow[i]) {
dis[to[i]] = dis[w] + 1;
que.push(to[i]);
}
}
}
return dis[T] < INF;
}
int DFS(int w, int f) {
if (w == T) return f;
else {
int ret = 0;
for (int tmp,&i=cur[w];i;i=nxt[i]) {
if (dis[to[i]] == dis[w] + 1 && flow[i]) {
tmp = DFS(to[i], min(f, flow[i]));
flow[i] -= tmp; flow[i^1] += tmp;
f -= tmp; ret += tmp;
if (!f) break;
}
}
return ret;
}
}
}Dinic;

class SurroundingGame {
int n,m,E,vout;
public:
int maxScore(vector<string> cost, vector<string> benefit) {
init();
m = cost.size();
n = cost[0].size();
for (int j=0;j<m;j++) {
for (int i=0;i<n;i++) {
vout += Val(benefit[j][i]);
}
}
for (int j=1;j<=m;j++) {
for (int i=1;i<=n;i++) {
if (i + j & 1) {
for (int k=1,x,y;k<=4;k++) {
x = i + dx[k];
y = j + dy[k];
if (1 <= x && x <= n && 1 <= y && y <= m) {
}
}
} else {
}
}
}
return vout - Dinic.MaxFlow();
}
private:
inline void init() {
E = 1; S = vout = 0; T = N - 1;
}
inline void Add_Edge(int u, int v, int f = INF) {
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0;
}
inline int id(int x, int y, int t) {
return ((y-1) * n + (x-1)) * 2 + t;
}
inline int Val(char c) {
if ('A' <= c && c <= 'Z') return c - 29;
if ('a' <= c && c <= 'z') return c - 87;
if ('0' <= c && c <= '9') return c - 48;
}
};


## 【TopCoder SRM570】Curvy on Rails

### 解题报告

1. 回路中的每一个点的度一定是 $2$
2. 如果每一个点的度都是 $2$ ，那么这一定是由一些回路构成的图

1. 横向的度
2. 纵向的度

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int L = 30;
const int N = 2000;
const int M = 50000;
const int INF = 1000000000;

int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1};

class Minimum_Cost_Flow{
int dis[N],sur[N],inq[N];
queue<int> que;
public:
inline pair<int,int> MaxFlow() {
int ret_cost = 0, ret_flow = 0;
for (int f=INF,w;SPFA();f=INF) {
for (w=T;w!=S;w=to[sur[w]^1]) f = min(f, flow[sur[w]]);
for (w=T;w!=S;w=to[sur[w]^1]) flow[sur[w]] -= f, flow[sur[w]^1] += f;
ret_cost += dis[T] * f;
ret_flow += f;
}
return make_pair(ret_cost, ret_flow);
}
private:
bool SPFA() {
memset(dis,60,sizeof(dis));
que.push(S); dis[S] = 0;

while (!que.empty()) {
int w = que.front(); que.pop(); inq[w] = 0;
if (dis[to[i]] > dis[w] + cost[i] && flow[i]) {
dis[to[i]] = dis[w] + cost[i];
sur[to[i]] = i;
if (!inq[to[i]]) inq[to[i]] = 1, que.push(to[i]);
}
}
}
return dis[T] < INF;
}
}MCMF;

class CurvyonRails {
int n,m,cnt,E;
char pat[L][L];
public:
int getmin(vector<string> field) {
init();
m = field.size();
n = field[0].size();
for (int j=0;j<m;j++) {
for (int i=0;i<n;i++) {
pat[i+1][j+1] = field[j][i];
}
}
for (int j=1;j<=m;j++) {
for (int i=1;i<=n;i++) {
if (pat[i][j] == 'w') continue;
else {
if (++cnt, i + j & 1) {
} else {
for (int k=1,x,y;k<=4;k++) {
x = i + dx[k];
y = j + dy[k];
if (1 <= x && x <= n && 1 <= y && y <= m) {
if (x == i) Add_Edge(id(i,j,1), id(x,y,1), 1);
if (y == j) Add_Edge(id(i,j,0), id(x,y,0), 1);
}
}
}
if (pat[i][j] == '.') {
}
if (pat[i][j] == 'C') {
}
}
}
}
pair<int,int> vout = MCMF.MaxFlow();
if (vout.second < cnt) return -1;
else return vout.first;
}
private:
inline int id(int x, int y, int t) {
return ((y-1)*n + (x-1)) * 2 + 1 + t;
}
inline void init() {
E = 1; S = cnt = 0; T = N - 1;
}
inline void Add_Edge(int u, int v, int f, int c = 0) {
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f; cost[E] = c;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0; cost[E] = -c;
}
};


## 【BZOJ 3144】[HNOI2013] 切糕

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 70000;
const int M = N << 2;
const int INF = 1e9;

int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

inline void Add_Edge(int u, int v, int f = INF) {
static int E = 1;
to[++E] = v; nxt[E] = head[u]; head[u] = E; flow[E] = f;
to[++E] = u; nxt[E] = head[v]; head[v] = E; flow[E] = 0;
}

char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline int id(int x, int y, int z) {
return ((y - 1) * q + x - 1) * (r + 1) + z;
}

class Network_Flow{
int cur[N],dis[N];
queue<int> que;
public:
inline int MaxFlow() {
int ret = 0;
while (BFS()) {
ret += DFS(S, INF);
}
return ret;
}
private:
inline bool BFS() {
memset(dis,60,sizeof(dis));
que.push(S); dis[S] = 0;

while (!que.empty()) {
int w = que.front(); que.pop();
if (dis[to[i]] > INF && flow[i]) {
dis[to[i]] = dis[w] + 1;
que.push(to[i]);
}
}
}
return dis[T] < INF;
}
int DFS(int w, int f) {
if (w == T) return f;
else {
int ret = 0;
for (int tmp,&i=cur[w];i;i=nxt[i]) {
if (dis[to[i]] == dis[w] + 1 && flow[i]) {
tmp = DFS(to[i], min(f, flow[i]));
flow[i] -= tmp; flow[i^1] += tmp;
f -= tmp; ret += tmp;
if (!f) break;
}
}
return ret;
}
}
}Dinic;

int main() {
S = 0; T = N - 1;
for (int z=1;z<=r;z++) {
for (int y=1;y<=p;y++) {
for (int x=1,v;x<=q;x++) {
for (int k=0,X,Y;k<4;k++) {
X = x + dx[k];
Y = y + dy[k];
if (1 <= X && X <= q && 1 <= Y && Y <= p) {
if (z > d) Add_Edge(id(x,y,z), id(X,Y,z-d));
if (z+d+1 <= r) Add_Edge(id(X,Y,z+d+1), id(x,y,z));
}
}
}
}
}
for (int x=1;x<=q;x++) {
for (int y=1;y<=p;y++) {
}
}
printf("%d\n",Dinic.MaxFlow());
return 0;
}


## 【BZOJ 3894】文理分科

1. 选理的费用
2. 同选理的费用

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 30000+9;
const int M = 300000+9;
const int INF = 1e9;

int dx[]={1,0,-1,0},dy[]={0,1,0,-1},vout;

char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline void Add_Edge(int u, int v, int f = INF) {
static int T = 1;
to[++T] = v; nxt[T] = head[u]; head[u] = T; flow[T] = f;
to[++T] = u; nxt[T] = head[v]; head[v] = T; flow[T] = 0;
}

inline int id(int x, int y, int t) {
return n * m * (t - 1) + (y - 1) * n + x;
}

class MaxFlow{
int cur[N],dis[N];
queue<int> que;
public:
inline int solve() {
int ret = 0;
while (BFS()) {
ret += DFS(S, INF);
}
return ret;
}
private:
inline bool BFS() {
memset(dis,60,sizeof(dis));
que.push(S); dis[S] = 0;

while (!que.empty()) {
int w = que.front(); que.pop();
if (dis[to[i]] > INF && flow[i]) {
dis[to[i]] = dis[w] + 1;
que.push(to[i]);
}
}
}
return dis[T] < INF;
}
int DFS(int w, int f) {
if (w == T) return f;
else {
int ret = 0;
for (int tmp,&i=cur[w];i;i=nxt[i]) {
if (dis[to[i]] == dis[w] + 1 && flow[i]) {
tmp = DFS(to[i], min(f, flow[i]));
flow[i] -= tmp; flow[i^1] += tmp;
f -= tmp; ret += tmp;
if (!f) break;
}
}
return ret;
}
}
}Dinic;

int main() {
S = 0; T = N - 1;
for (int j=1,tmp;j<=m;j++)
for (int i=1;i<=n;i++)
for (int j=1,tmp;j<=m;j++)
for (int i=1;i<=n;i++)
for (int j=1,tmp;j<=m;j++)
for (int i=1;i<=n;i++)
for (int j=1,tmp;j<=m;j++)
for (int i=1;i<=n;i++)
for (int j=1,w=0;j<=m;j++) {
for (int i=1;i<=n;i++) {
for (int k=0,x,y;k<4;k++) {
x = i + dx[k];
y = j + dy[k];
if (1 <= x && x <= n && 1 <= y && y <= m) {
}
}
}
}
printf("%d\n",vout-Dinic.solve());
return 0;
}