Homework Introduction

#include <bits/stdc++.h>
using namespace std;
/*
EK
1.找一条从s~t的路径
2.顺着路径减掉对应流量,反边加回去
*/
const int N = 205, M = 505;
int n, m, vis[N];

struct node {
	int nxt, to, flow;
} e[M];

struct point {
	int v, flow;
};
int head[N], cnt = -1, pre[N], s, t, id[N];

void add(int x, int y, int z) {
	++cnt;
	e[cnt].nxt = head[x];
	e[cnt].to = y;
	e[cnt].flow = z;
	head[x] = cnt;
}

int bfs(int s) {
	memset(vis, 0, sizeof(vis));
	queue<point>q;
	q.push((point) {
		s, 1000000000
	});
	vis[s] = 1;
	while (!q.empty()) {
		point tmp = q.front();
		q.pop();
		if (tmp.v == t) {
			return tmp.flow;
		}
		for (int i = head[tmp.v]; i != -1; i = e[i].nxt) {
			int v = e[i].to;
			int flow = e[i].flow;
			if (flow <= 0)
				continue;
			if (!vis[v]) {
				//cout << tmp.v << " " << v << " " << flow << " " << tmp.flow << endl;
				vis[v] = 1;
				q.push(point{v, min(flow, tmp.flow)});
				pre[v] = tmp.v;
				id[v] = i;
			}
		}
	}
	return 0;
}

int main() {
	cin >> m >> n;
	s = 1, t = n;
	memset(head, -1, sizeof(head));
	for (int i = 1; i <= m; i++) {
		int x, y, z;
		cin >> x >> y >> z;
		add(x, y, z);
		add(y, x, 0);
	}
	int res = 0;
	while (1) {
		int c = bfs(s);
		//cout << c << endl;
	//	cout << "-----------" << endl;
		if (c == 0)
			break;
		int tmp = t;
		while (tmp != s) {
			int ID = id[tmp];
			e[ID].flow -= c;
			e[ID ^ 1].flow += c;
			tmp = pre[tmp];
		}
		res += c;
	}
	cout << res << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 205,M=200005;
#define int long long
struct node{
	int nxt,to,flow;
}e[M];
int n,m,s,t,now[N];
int head[N],cnt=-1,dep[N];
void add(int x,int y,int z){
	e[++cnt].nxt = head[x];
	e[cnt].to = y;
	e[cnt].flow = z;
	head[x] = cnt;
}
int bfs(){
	memset(dep,0,sizeof(dep));
	dep[s] = 1;
	now[s] = head[s];
	queue<int>q;
	q.push(s);
	while(!q.empty()){
		int tmp = q.front();
		q.pop();
		for(int i=head[tmp];i!=-1;i=e[i].nxt){
			int v = e[i].to;
			int w = e[i].flow;
			if(!dep[v] && w>0){
				dep[v] = dep[tmp]+1;
				q.push(v);
				now[v] = head[v];
			}
		}
	}
	return dep[t];
}
int dfs(int x,int flow){
	if(x==t)return flow;
	for(int i=now[x];i!=-1;i=e[i].nxt){
		int v = e[i].to;
		now[x] = i;
		int Flow = e[i].flow;
		if(dep[v]==dep[x]+1 && Flow>0){
			int c = dfs(v,min(flow,Flow));
			if(c>0){
				e[i].flow-=c;
				e[i^1].flow+=c;
				return c;
			}
		}
	}
	return 0;
}
int dinic(){
	int res = 0;
	while(bfs()!=0){
		int c;
		while(c=dfs(s,1000000000)){
			res+=c;
		}
	}
	return res;
}
signed main()
{
	memset(head,-1,sizeof(head));
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z;
		add(x,y,z);
		add(y,x,0);
	}
	cout<<dinic()<<endl;
	return 0;
}
Status
Done
Problem
6
Open Since
2026-1-15 0:00
Deadline
2026-1-23 23:59
Extension
24 hour(s)