zoukankan      html  css  js  c++  java
  • 洛谷 P3376 【模板】网络最大流

    题目链接

    保存一下dinic的板子 (不开longlong见祖宗)

    #include <bits/stdc++.h>
    using namespace std;
    const int INF=0x7fffffff;
    const int MAXN = 210,MAXM = 10010;
    namespace maxflow {
        typedef long long type;
        const type INF=0x7fffffff;
        struct node {
            int to; type cap; int next;
            node(int t=0,type c=0,int n=0):to(t),cap(c),next(n) {};
        } edge[MAXM];
        int head[MAXN],tot;
        void addedge(int from,int to,type cap,type rcap=0) {
            edge[tot]=node(to,cap,head[from]); head[from]=tot++;
            edge[tot]=node(from,rcap,head[to]); head[to]=tot++;
        }
        int dep[MAXN],cur[MAXN];//当前弧优化
        bool bfs(int s,int t,int n) {
            static int Q[MAXN],ST,ED;
            memset(dep+1,0,n*sizeof(int));
            ST=0; ED=-1;
            Q[++ED]=s; dep[s]=1;
            while (ST<=ED) {
                int u=Q[ST++];
                for (int i=head[u]; i!=-1; i=edge[i].next) {
                    int v=edge[i].to;
                    if (!dep[v]&&edge[i].cap) {
                        Q[++ED]=v; dep[v]=dep[u]+1;
                    }
                }
            } return (dep[t]!=0);
        }
        type dfs(int x,const int &t,type flow=INF) {
            if (x==t||flow==0) return flow;
            type ret=0;
            for (int i=cur[x]; i!=-1; i=edge[i].next) {
                if (dep[x]+1==dep[edge[i].to]&&edge[i].cap){
                    type f=dfs(edge[i].to,t,min(flow,edge[i].cap));
                    edge[i].cap-=f; edge[i^1].cap+=f;
                    ret+=f; flow-=f; cur[x]=i;
                    if (flow==0) break;
                }
            } if (!ret) dep[x]=0;
            return ret;
        }
        type maxflow(int s,int t,int n) {//Dinic
            type ret=0;
            while (bfs(s,t,n)) {
                type f;
                memcpy(cur+1,head+1,n*sizeof(int));
                while ((f=dfs(s,t))>0) ret+=f;
            } return ret;
        }
        void init(int n) {
            memset(head+1,0xff,n*sizeof(int)); tot=0;
        }
    }
    
    int main(){
        int n,m,s,t;
        cin>>n>>m>>s>>t;
        maxflow::init(n);
        for(int i=0,u,v,w;i<m;i++){
            cin>>u>>v>>w;
            maxflow::addedge(u,v,w);
        }
        cout<<maxflow::maxflow(s,t,n)<<"
    ";
        return 0;
    }
    
  • 相关阅读:
    V-Ray Material Library材质名称翻译
    3dMax常用快捷键
    3dMax笔记(韵湖)
    background-size拉伸背景图片
    CSS实现子元素水平垂直居中的6种方式
    JS简易实现“最小栈”
    JS种Array原型方法reverse的模拟实现
    JS数组去重的3种方式
    encodeURIComponent和encodeURI有什么区别
    CSS中的度量单位(px/em/rem/vm/vh/...)
  • 原文地址:https://www.cnblogs.com/charles1999/p/13324531.html
Copyright © 2011-2022 走看看