萌新刚学 OI,调不动了,求助 37pts 的 Dinic
查看原帖
萌新刚学 OI,调不动了,求助 37pts 的 Dinic
298549
SIXIANG32楼主2021/6/1 21:48

又 WA 又 TLE 了,Dinic 磨人,救救孩子!/kk

#include <iostream>
#include <queue>
#include <cstring>
#define int long long
#define MAXN 100000
#define QWQ cout << "QWQ" << endl;
using namespace std;
int min(int x, int y) {return ((x < y) ? (x) : (y));}
struct node {
	int id, val;
	int next;
} gra[MAXN + 10];
int n, m, s, t;
int tot = 0, head[MAXN + 10], dis[MAXN + 10], Now[MAXN + 10];
void link(int x, int y, int z) {
	gra[++tot].id = y, gra[tot].val = z;
	gra[tot].next = head[x], head[x] = tot;
	gra[++tot].id = x, gra[tot].val = 0;
	gra[tot].next = head[y], head[y] = tot;
}
bool bfs() { 
	memset(dis, 0, sizeof(dis));
	queue <int> que;
	que.push(s), dis[s] = 1, Now[s] = head[s];
	while(!que.empty()) {
		int fr = que.front(); que.pop();
		for(int p = head[fr]; p; p = gra[p].next) {
			int v = gra[p].id, w = gra[p].val;
			if(w && !dis[v]) {
				que.push(v);
				Now[v] = head[v];
				dis[v] = dis[fr] + 1;
				if(v == t) return 1;
			}
		}
	}
	return 0;
}
int Dinic(int u, int over) {
	if(u == t) return over;
	int p, res = over, k;
	for(p = Now[u]; p && res; p = gra[p].next) {
		int v = gra[p].id, w = gra[p].val;
		Now[u] = v; 
		if(w && dis[v] == dis[u] + 1) {
			k = Dinic(v, min(res, w));
			if(!k) dis[v] = 0x7f7f7f7f;
			gra[p].val -= k;
			gra[p ^ 1].val += k;
			res -= k;
		}
	}
	return over - res;
}
signed main() {
	cin >> n >> m >> s >> t;
	for(int p = 1, x, y, z; p <= m; p++) {
		cin >> x >> y >> z;
		link(x, y, z);
	}
	int maxn = 0, rest = 0;
	while(bfs())
		maxn += (rest = Dinic(s, 0x7f7f7f7f)), cout << rest << endl;
	cout << maxn << endl;
}

求助/kel

2021/6/1 21:48
加载中...