HzuWHF


私信TA

用户名:I7I08I9047

访问量:76448

签 名:

我RUN了

等  级
排  名 18
经  验 20463
参赛次数 13
文章发表 127
年  龄 3
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

        链式向前星存图,BFS 构造分层图,DFS 寻找增广路  HUD 3549


#include<bits/stdc++.h>
#define Inf 0x3F3F3F3F
using namespace std;

const int SIZE = (int)(1E5 + 7);
typedef struct {
	int next, to, w;
} Edge;
Edge edge[SIZE << 1];
int head[SIZE << 1], Deep[SIZE], cnt, aimT;
queue<int> que;

void add(int form, int to, int w) {
	edge[cnt].to = to; 
	edge[cnt].w = w;
	edge[cnt].next = head[form];
	head[form] = cnt++;
}

int BFS(int S, int T) {
	memset(Deep, 0, sizeof Deep);
	while (!que.empty()) que.pop();
	Deep[S] = 1; que.push(S);
	while (!que.empty()) {
		int now = que.front(); que.pop();
		for (int pos = head[now]; ~pos; pos = edge[pos].next)
			if (edge[pos].w && !Deep[edge[pos].to]) {
				Deep[edge[pos].to] = Deep[now] + 1;
				que.push(edge[pos].to);
			}
	}
	return Deep[T];
}

int DFS(int now, int rem) {
	if (now == aimT) return rem;
	int all_flow = 0;
	for (int pos = head[now]; ~pos && rem; pos = edge[pos].next) {
		if (edge[pos].w && Deep[edge[pos].to] == Deep[now] + 1) {
			int add_flow = DFS(edge[pos].to, min(rem, edge[pos].w));
			
			edge[pos].w -= add_flow; 
			edge[pos ^ 1].w += add_flow;
			
			rem -= add_flow; all_flow += add_flow;
		}
	}
	
	if (all_flow == 0)
		Deep[now] = -1;
	return all_flow;
}

int Dinic(int S, int T) {
	aimT = T; int ans = 0;
	while (BFS(S, T))
		ans += DFS(S, Inf);
	return ans;
}

int main() {
	int form, to, w, path, total, point;
	scanf("%d", &total);
	
	for(int ind = 1; ind <= total; ind++){
		memset(head, -1, sizeof head), cnt = 0;
		
		scanf("%d%d", &point, &path);
		while (path--) {
			scanf("%d%d%d", &form, &to, &w);
			add(form, to, w); add(to, form, 0);
		}
		printf("Case %d: %d\n", ind, Dinic(1, point));
	}
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区