网络流 (EK Dinic)

  • Edmonds-Karp
    BFS找增广路,找到增广路,更新残余网络。
// 临接矩阵
// pre记录前驱节点
int G[maxn][maxn], pre[maxn], vis[maxn], n, m;
// BFS找增广路
bool bfs(int s, int e) {
	memset(vis, 0, sizeof(vis));
	memset(pre, -1, sizeof(pre));
	pre[s] = s;
	vis[s] = 1;
	queue<int> que;
	que.push(s);
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		// 访问u所有的出边, 判断能否增广
		for (int i = 0; i <= e; i++) {
			if (G[u][i] && !vis[i]) {
				pre[i] = u;
				que.push(i);
				vis[i] = 1;
				if (i == e)	return 1; // 到汇点存在增广路
			}
		}
	}
	return 0;
}
int EK(int s, int e) {
	int ans = 0;
	while (bfs(s, e)) { // 判断是否存在增广路
		int MIN = inf;
		// 遍历增广路上所有边取流量最小的边
		for (int i = e; i != s; i = pre[i]) {
			MIN = min(MIN, G[pre[i]][i]);
		}
		// 更新增广路上的流量, 正向减,反向加
		for (int i = e; i != s; i = pre[i]) {
			G[pre[i]][i] -= MIN;
			G[i][pre[i]] += MIN;
		}
		ans += MIN;
	}
	return ans;
}
int cnt;
struct ac{
	int v, c, nex;
}edge[maxn << 8];
int vis[maxn], head[maxn], path[maxn];
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int c) {
	// 正向边
	edge[cnt] = {v, c, head[u]};
	head[u] = cnt++;
	// 反向边
	edge[cnt] = {u, 0, head[v]};
	head[v] = cnt++;
}
bool bfs(int s, int t) {
	memset(vis, 0, sizeof(vis));
	memset(path, -1, sizeof(path));
	queue<int> que;
	que.push(s);
	vis[s] = 1;
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		// 遍历u的所有出边
		for (int i = head[u]; i != -1; i = edge[i].nex) {
			int v = edge[i].v;
			int c = edge[i].c;
			// 判断能否增广
			if (c == 0 || vis[v]) continue;
			path[v] = i;
			vis[v] = 1;
			que.push(v);
			if (v == t) return true;
		}
	}
	return false;
}
int EK(int s, int t) {
	int ans = 0;
	while (bfs(s, t)) {
		int flow = inf;
		for (int i = path[t]; i != -1; i = path[edge[i^1].v]) {
			flow = min(flow, edge[i].c);
		}
		for (int i = path[t]; i != -1; i = path[edge[i^1].v]) {
			edge[i].c -= flow;
			edge[i^1].c += flow;
		}	
		ans += flow;
	}
	return ans;
}
  • Dinic
struct ac{
    int v, c, nex;
}edge[maxn << ];
int s, e;
int head[maxn], dis[maxn], curedge[maxn], cnt;
void init() {
    cnt = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int c) {
	// 正向建边
    edge[cnt] = {v, c, head[u]};
    head[u] = cnt++;
    // 反向建边, 流量为0
    edge[cnt] = {u, 0, head[v]};
    head[v] = cnt++;
}
bool bfs() {
    queue<int> que;
    que.push(s);
    memset(dis, 0, sizeof(dis)); // 对图进行分层
    dis[s] = 1;
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (int i = head[u]; i != -1; i = edge[i].nex) {
        	int v = edge[i].v;
        	int c = edge[i].c;
        	// 如果节点v已经分过层或者u->v流量为0, continue
            if (dis[v] || c == 0) continue;
            dis[v] = dis[u] + 1; // 对v进行标记并加入队列
            que.push(v);
        }
    }
    return dis[e] > 0;  // 判断是否存在增广路,s是否能到达e
}

int dfs(int u, int flow) { // 增广路走到u点的最小流量为flow
    if (u == e || flow == 0) return flow;
    // 遍历u的所有出边
    for (int &i = curedge[u]; i != -1; i = edge[i].nex) { // 当前弧优化
    	int v = edge[i].v;
    	int c = edge[i].c;
    	// 判断能否u->v增广
        if (dis[v] != dis[u] + 1 || c == 0) continue;
        int d = dfs(v, min(flow, c));
        if (d > 0) { // 找到一条增广路,修改增广路上的正反向边
            edge[i].c -= d;
            edge[i^1].c += d;
            return d;
        }            
    }
    dis[u] = -1; // // 炸点优化
    return 0;
}
int Dinic() {
    int sum = 0, d;
    while (bfs()) { // 判读是否存在增广路
        for (int i = 0; i <= e; ++i) curedge[i] = head[i]; // copy head数组,在dfs中可以直接得到下一条没有被增广过的边
        while ((d = dfs(s, inf)) > 0) sum += d; // 多次dfs找增广路
    }
    return sum;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值