• 初学网络流——最大流模板(dinic)


    多组输入只需把vector清零即可
    o( ̄︶ ̄)o

    #include<bits/stdc++.h>
    
    using namespace std;
    
    typedef long long ll;
    typedef pair<int,int> PII;
    const int MAXN = 1e4+10;
    const double EPS = 1e-12;
    
    int n,m,s,t,cas,T;
    struct edge{
        int to,cap,rev;
    };
    vector<edge>G[MAXN];
    int level[MAXN],iter[MAXN];
    
    inline void addedge(int from,int to,int cap){
        G[from].push_back(edge{to,cap,G[to].size()});
        G[to].push_back(edge{from,0,G[from].size()-1});
    }
    
    inline bool bfs(){
        memset(level,0,sizeof(level));
        queue<int>q;
        level[s]=1;
        q.push(s);
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();i++){
                edge &e=G[x][i];
                if(e.cap>0&&level[e.to]==0){
                    level[e.to]=level[x]+1;
                    q.push(e.to);
                }
            }
        }
        return level[t]==0 ? 0 : 1;
    }
    
    inline int dfs(int x,int flow){
        if(x==t)return flow;
        for(int &i=iter[x];i<G[x].size();i++){
            edge &e=G[x][i];
            if(e.cap>0&&level[e.to]>level[x]){
                int d=dfs(e.to,min(flow,e.cap));
                if(d>0){
                    e.cap-=d;
                    G[e.to][e.rev].cap+=d;
                    return d;
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        scanf("%d %d %d %d",&n,&m,&s,&t);
        int u,v,w;
        for(int i=1;i<=m;i++){
            scanf("%d %d %d",&u,&v,&w);
            addedge(u,v,w);
        }
        int ans=0;
        while(bfs()){
            memset(iter,0,sizeof(iter));
            int now;
            while((now=dfs(s,1e9)))
                ans+=now;
        }
        printf("%d
    ",ans);
    }
    
    
  • 相关阅读:
    Android 逐帧动画
    MAP getLastKnownLocation()返回null的解决
    大数取余
    (a^b)%c和(a/b)%c
    HDU1046 Gridland
    顺序入栈的出栈方法种数
    HDU1021 Fibonacci Again
    HDU1019 Least Common Multiple
    HDU1018 Big Number
    HDU1013 Digital Roots
  • 原文地址:https://www.cnblogs.com/Mmasker/p/11917457.html
Copyright © 2020-2023  润新知