Ford-Fulkerson代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 105;
const int M = N * N * N * 2;

struct Edge {
int to, nxt, cap, rev;
Edge(int to, int nxt, int cap, int rev) : to(to), nxt(nxt), cap(cap), rev(rev) {}
Edge() {}
};
Edge e[M];

void addEdge(int from, int to, int cap, int rev, int cnt) {
e[cnt] = Edge(to, head[from], cap, rev);
}
void addEdgePair(int from, int to, int cap, int &cnt) {
cnt += 2;
}
void init(int n) {
for (int i = 0; i <= n; i++) head[i] = -1;
}

bool vis[N*4];

int dfs(int x, int en, int flow) {
if (x == en) return flow;
vis[x] = true;
for (int i = head[x]; ~i; i = e[i].nxt) {
int to = e[i].to, rev = e[i].rev;
if (!vis[to] && e[i].cap > 0) {
int f = dfs(to, en, min(flow, e[i].cap));
if (f > 0) {
e[i].cap -= f;
e[rev].cap += f;
return f;
}
}
}
return 0;
}

int maxFlow(int st, int en) {
int flow = 0;
while (true) {
memset(vis, 0, sizeof vis);
if (!dfs(st, en, 1e9)) break;
flow++;
}
return flow;
}

int main() {
int n, f, d, fn, dn, x;
while (scanf("%d%d%d", &n, &f, &d) == 3) {
int ec = 0, st = 2 * n + f + d, en = st + 1;
init(en);

for (int i = 0; i < f; i++) {
addEdgePair(st, 2 * n + i, 1, ec);
}
for (int i = 0; i < d; i++) {
addEdgePair(2 * n + f + i, en, 1, ec);
}
for (int i = 0; i < n; i++) {
addEdgePair(i, n + i, 1, ec);
scanf("%d%d", &fn, &dn);
for (int j = 0; j < fn; j++) {
scanf("%d", &x);
x--;
addEdgePair(2 * n + x, i, 1, ec);
}
for (int j = 0; j < dn; j++) {
scanf("%d", &x);
x--;
addEdgePair(n + i, 2 * n + f + x, 1, ec);
}
}
printf("%d\n", maxFlow(st, en));
}
return 0;
}


Dinic代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 105;
const int M = N * N * N * 2;

struct Edge {
int to, nxt, cap, rev;
Edge(int to, int nxt, int cap, int rev) : to(to), nxt(nxt), cap(cap), rev(rev) {}
Edge() {}
};
Edge e[M];

void addEdge(int from, int to, int cap, int rev, int cnt) {
e[cnt] = Edge(to, head[from], cap, rev);
}
void addEdgePair(int from, int to, int cap, int &cnt) {
cnt += 2;
}
void init(int n) {
for (int i = 0; i <= n; i++) head[i] = -1;
}

int level[N*4];
int iter[N*4];

bool bfs(int st, int en) {
level[st] = 0;
queue<int> q;
q.push(st);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = head[cur]; ~i; i = e[i].nxt) {
int to = e[i].to;
if (level[to] == -1 && e[i].cap > 0) {
q.push(to);
level[to] = level[cur] + 1;
}
}
}
}

int dfs(int x, int en, int flow) {
if (x == en) return flow;
for (int &i = iter[x]; ~i; i = e[i].nxt) {
int to = e[i].to, rev = e[i].rev;
if (e[i].cap == 0 || level[to] <= level[x]) continue;

int f = dfs(to, en, min(flow, e[i].cap));
if (f > 0) {
e[i].cap -= f;
e[rev].cap += f;
return f;
}
}
return 0;
}

int maxFlow(int st, int en) {
int flow = 0;
while (true) {
memset(level, -1, sizeof level);
bfs(st, en);
if (level[en] == -1) break;
while (dfs(st, en, 1)) {
flow++;
}
}
return flow;
}

int main() {
int n, f, d, fn, dn, x;
while (scanf("%d%d%d", &n, &f, &d) == 3) {
init(2 * n + d + f + 2);
int ec = 0;
int st = 2 * n + f + d, en = st + 1;
int bef = 2 * n, bed = 2 * n + f;
for (int i = 0; i < f; i++) {
addEdgePair(st, bef + i, 1, ec);
}
for (int i = 0; i < d; i++) {
addEdgePair(bed + i, en, 1, ec);
}
for (int i = 0; i < n; i++) {
addEdgePair(i, n + i, 1, ec);
scanf("%d%d", &fn, &dn);
for (int j = 0; j < fn; j++) {
scanf("%d", &x);
x--;
addEdgePair(bef + x, i, 1, ec);
}
for (int j = 0; j < dn; j++) {
scanf("%d", &x);
x--;
addEdgePair(n + i, bed + x, 1, ec);
}
}
printf("%d\n", maxFlow(st, en));
}
return 0;
}

• 点赞 1
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

ned_chu

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
04-14 596

09-02 25
08-07 101
04-22