贴一个最大流板子
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
const int maxn = 810;
const int maxm = 640010;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
int S, T;
struct edge {
int to, cap, rev;
edge() {}
edge(int t, int cap, int rev) :to(t), cap(cap), rev(rev) {}
};
vector <edge> _maxflow_G[maxm];
int _maxflow_dis[maxm];
int _maxflow_cur[maxm];
void _maxflow_init(){
for(int i = 0; i < maxn; i++) _maxflow_G[i].clear();
}
void _maxflow_addedge(int from, int to, int cap) {
_maxflow_G[from].push_back(edge(to, cap, _maxflow_G[to].size()));
_maxflow_G[to].push_back(edge(from, 0, _maxflow_G[from].size() - 1));
}
void _maxflow_bfs(int s) {
memset(_maxflow_dis, -1, sizeof(_maxflow_dis));
queue <int> q;
_maxflow_dis[s] = 0;
q.push(s);
while (!q.empty()) {
int v = q.front(); q.pop();
for (int i = 0; i < _maxflow_G[v].size(); i++) {
edge &e = _maxflow_G[v][i];
if (e.cap > 0 && _maxflow_dis[e.to] < 0) {
_maxflow_dis[e.to] = _maxflow_dis[v] + 1;
q.push(e.to);
}
}
}
}
int _maxflow_dfs(int v, int t, int f) {
if (v == t) return f;
for (int &i = _maxflow_cur[v]; i < _maxflow_G[v].size(); i++) {
edge &e = _maxflow_G[v][i];
if (e.cap > 0 && _maxflow_dis[v] < _maxflow_dis[e.to]) {
int d = _maxflow_dfs(e.to, t, min(f, e.cap));
if (d > 0) {
e.cap -= d;
_maxflow_G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int maxflow(int s, int t) {
int flow = 0;
for (;;) {
_maxflow_bfs(s);
if (_maxflow_dis[t] < 0) return flow;
memset(_maxflow_cur, 0, sizeof(_maxflow_cur));
int f;
while ((f = _maxflow_dfs(s, t, INF)) > 0) {
flow += f;
}
}
return flow;
}
int a[maxn];
int n, m;
int main() {
freopen("in.txt", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF) {
scanf("%d%d", &S, &T);
_maxflow_init();
_rep(i, 1, n) {
int x;
scanf("%d", &x);
_maxflow_addedge(i, n + i, x);
}
_rep(i, 1, m) {
int u, v;
scanf("%d%d", &u, &v);
_maxflow_addedge(n + u, v, inf);
_maxflow_addedge(n + v, u, inf);
}
printf("%d\n", maxflow(S, n + T));
}
return 0;
}
另一个
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
const int maxn = 810;
const int maxm = 640010;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
int S, T;
int n, m;
struct _maxflow_edge {
int v, c, pre;
}_maxflow_edges[20001 * 4];
int _maxflow_head[maxm], _maxflow_dis[maxm], _maxflow_cur[maxm], _maxflow_cnt;
void _maxflow_init() {
_maxflow_cnt = 0;
memset(_maxflow_head, -1, sizeof(_maxflow_head));
}
void _maxflow_addedge(int u, int v, int c) {
_maxflow_edges[_maxflow_cnt].v = v;
_maxflow_edges[_maxflow_cnt].c = c;
_maxflow_edges[_maxflow_cnt].pre = _maxflow_head[u];
_maxflow_head[u] = _maxflow_cnt++;
swap(u, v);
_maxflow_edges[_maxflow_cnt].v = v;
_maxflow_edges[_maxflow_cnt].c = 0;
_maxflow_edges[_maxflow_cnt].pre = _maxflow_head[u];
_maxflow_head[u] = _maxflow_cnt++;
}
bool bfs(int s, int t) {
queue<int> que;
que.push(s);
memset(_maxflow_dis, 0, sizeof(_maxflow_dis));
_maxflow_dis[s] = 1;
while (!que.empty()) {
int t = que.front();
que.pop();
for (int i = _maxflow_head[t]; i != -1; i = _maxflow_edges[i].pre) {
if (_maxflow_dis[_maxflow_edges[i].v] || _maxflow_edges[i].c == 0) continue;
_maxflow_dis[_maxflow_edges[i].v] = _maxflow_dis[t] + 1;
que.push(_maxflow_edges[i].v);
}
}
return _maxflow_dis[t] != 0;
}
int dfs(int s, int t, int flow) {
if (s == t) return flow;
for (int &i = _maxflow_cur[s]; i != -1; i = _maxflow_edges[i].pre) {
if (_maxflow_dis[_maxflow_edges[i].v] == _maxflow_dis[s] + 1 && _maxflow_edges[i].c) {
int d = dfs(_maxflow_edges[i].v, t, min(flow, _maxflow_edges[i].c));
if (d > 0) {
_maxflow_edges[i].c -= d;
_maxflow_edges[i ^ 1].c += d;
return d;
}
}
}
return 0;
}
int maxflow(int s, int t) {
int sum = 0;
while (bfs(s, t)) {
memcpy(_maxflow_cur, _maxflow_head, sizeof(_maxflow_cur));
int d;
while (d = dfs(s, t, inf)) {
sum += d;
}
}
return sum;
}