最大流模板(ek和dinic)

EK

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
const double pi = acos(-1);
const int maxn = 1e4 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
int ans, cnt, m, n, s, t;
int head[maxn], vis[maxn];
struct node{
    int to;
    int next;
    int quan;
}edge[maxm << 1];
struct nod{
    int uu;
    int eg;
}pre[maxn];
void add(int u, int v, int w){
    edge[cnt].to = v;
    edge[cnt].quan = w;
    edge[cnt].next = head[u];
    head[u] = cnt ++;
}
bool bfs(){
    queue<int> que;
    memset(vis, 0, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    vis[s] = 1;
    que.push(s);
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int i = head[u] ; i != -1 ; i = edge[i].next){
            int v = edge[i].to;
            if(!vis[v] && edge[i].quan){
                pre[v].uu = u;
                pre[v].eg = i;
                if(v == t) return true;
                vis[v] = 1;
                que.push(v);
            }
        }
    }
    return false;
}
int main()
{
    int u, v, w;
    scanf("%d %d %d %d", &n, &m, &s, &t);
    memset(head, -1, sizeof(head));
    for(int i = 1 ; i <= m ; ++ i){
        scanf("%d %d %d", &u, &v, &w);
        add(u, v, w); add(v, u, 0);
    }
    while(bfs()){
        int mini = INF;
        for(int i = t ; i != s ; i = pre[i].uu){
            mini = min(mini, edge[pre[i].eg].quan);
        }
        for(int i = t ; i != s ; i = pre[i].uu){
            edge[pre[i].eg].quan -= mini;
            edge[pre[i].eg^1].quan += mini;
        }
        ans += mini;
    }
    printf("%d", ans);
    return 0;
}

Dinic

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define endl '\n'
#define clr_0(a) memset(a, 0, sizeof(a))
#define clr_INF(a) memset(a, INF, sizeof(a))
#define lowbit(x) x & -x
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define PB push_back
#define POP pop_back
const double pi = acos(-1);
const int maxn = 2e5;
const int maxm = 4e5;
const ll mod = 998244353;
const int hash_mod = 19260817;
int n, m, s, t, cnt, maxflow;
int head[maxn], dep[maxn], cur[maxn], inq[maxn];
struct node{
    int to, next, quan;
}edge[maxm << 1];
struct nod{
    void add(int u, int v, ll w){
        edge[cnt].to = v;
        edge[cnt].quan = w;
        edge[cnt].next = head[u];
        head[u] = cnt ++;
    }
    bool bfs(){
        for(int i = 1 ; i <= n ; ++ i) cur[i] = head[i], dep[i] = INF, inq[i] = 0;
        queue<int> que;
        que.push(s);
        inq[s] = 1;
        dep[s] = 0;
        while(!que.empty()){
            int u = que.front();
            que.pop();
            inq[u] = 0;
            for(int i = head[u] ; ~ i ; i = edge[i].next){
                int v = edge[i].to;
                if(edge[i].quan && dep[v] > dep[u] + 1){
                    dep[v] = dep[u] + 1;
                    if(!inq[v]){
                        inq[v] = 1;
                        que.push(v);
                    }
                }
            }
        }
        return dep[t] == INF ? 0 : 1;
    }
    int dfs(int u, int flow){
        int rlow = 0;
        if(u == t){
            maxflow += flow;
            return flow;
        }
        int used = 0;
        for(int i = cur[u] ; ~ i ; i = edge[i].next){
            cur[u] = i;
            int v = edge[i].to;
            if(edge[i].quan && dep[v] == dep[u] + 1){
                if(rlow = dfs(v, min(flow - used, edge[i].quan))){
                    used += rlow;
                    edge[i].quan -= rlow;
                    edge[i^1].quan += rlow;
                    if(used == flow) break;
                }
            }
        }
        return used;
    }
    void solve(){
        while(bfs()){
            dfs(s, INF);
        }
    }
}dinic;
int main()
{
    int u, v, w, T;
    maxflow = 0;
    cnt = 0;
    scanf("%d %d %d %d", &n, &m, &s, &t);
    memset(head, -1, sizeof(head));
    for(int i = 1 ; i <= m ; ++ i){
        scanf("%d %d %d", &u, &v, &w);
        dinic.add(u, v, w); dinic.add(v, u, 0);
    }
    dinic.solve();
    cout << maxflow << endl;
    return 0;
}

你可能感兴趣的:(最大流模板(ek和dinic))