今天开始认真地做网络流的题,结果被自己之前在白书上学的那个dinic模板坑傻了,又WA又CE真是一时爽,还可以直接把编译器卡爆TAT,我对STL顿时感觉无爱了,果断弃坑写了个非STL的Dinic模板。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
#define maxn 100 + 10
#define maxm 10000 + 10
#define INF 0x7f7f7f7f
struct edge{
int to, next, cap;
}e[maxm];
int n, m, s, t, cur = 1, ans = 0, h[maxn], que[maxn], front[maxn], pa[maxn];
inline void addedge(int u,int v,int c){
cur++;
e[cur].to = v;
e[cur].cap = c;
e[cur].next = front[u];
front[u] = cur;
cur ++;
e[cur].to = u;
e[cur].next = front[v];
front[v] = cur;
}
inline bool bfs(){
int x, tp = 0, d = 1, p;
memset(h, -1, sizeof(h));
que[tp] = h[0] = 0;
while(tp < d){
x = que[tp]; tp ++;
int tmp = front[x];
while(tmp){
if(e[tmp].cap && h[e[tmp].to] < 0){
que[d ++] = e[tmp].to;
h[e[tmp].to] = h[x] + 1;
}
tmp = e[tmp].next;
}
}
if(h[t] == -1) return 0;
return 1;
}
inline int dfs(int x, int min_adv){
if(x == t) return min_adv;
int tmp = front[x];
int flow = 0, f;
while(tmp){
if(e[tmp].cap && h[e[tmp].to] == h[x] + 1){
f = min_adv - flow;
f = dfs(e[tmp].to, min(f, e[tmp].cap));
if(f) pa[x] = e[tmp].to;
e[tmp].cap -= f;
e[tmp ^ 1].cap += f;
flow += f;
if(flow == min_adv) return min_adv;
}
tmp = e[tmp].next;
}
if(! flow) h[x] = -1;
return flow;
}
inline void dinic(){
while(bfs()) ans += dfs(0,INF);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("dinic.in","r",stdin);freopen("dinic.out","w",stdout);
#endif
scanf("%d%d%d%d",&n,&m,&s,&t);
while(m --){
int u, v, c; scanf("%d%d%d",&u,&v,&c);
addedge(u, v, c);
}
dinic();
printf("%d\n",ans);
#ifndef ONLINE_JUDGE
fclose(stdin); fclose(stdout);
#endif
return 0;
}
这个代码非常好调,然而并没有加那个什么当前弧优化,可能我对这个优化理解不够透彻,之前就是这一块在报错也找不出来,然而据大神说这个优化用的比较少,那么就等这个被卡掉的那一天在学这东西吧~~~笑QAQ
接着网络流24题要好好推了~