• 洛谷P3376 【模板】网络最大流


    从今天开始学习网络流.jpg

    用的Dinic

    复杂度O(能过)

     1 //minamoto
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<cstring>
     6 #include<queue>
     7 using namespace std;
     8 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
     9 char buf[1<<21],*p1=buf,*p2=buf;
    10 inline int read(){
    11     #define num ch-'0'
    12     char ch;bool flag=0;int res;
    13     while(!isdigit(ch=getc()))
    14     (ch=='-')&&(flag=true);
    15     for(res=num;isdigit(ch=getc());res=res*10+num);
    16     (flag)&&(res=-res);
    17     #undef num
    18     return res;
    19 }
    20 char sr[1<<21],z[20];int C=-1,Z;
    21 inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
    22 inline void print(int x){
    23     if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
    24     while(z[++Z]=x%10+48,x/=10);
    25     while(sr[++C]=z[Z],--Z);sr[++C]='
    ';
    26 }
    27 const int inf=0x3f3f3f3f,N=10005,M=200005;
    28 int n,m,u,v,e,mxflow,dep[N];
    29 int head[N],Next[M],ver[M],edge[M];
    30 int tot=1,cur[N],s,t;
    31 queue<int> q;
    32 inline void add(int u,int v,int e){
    33     ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
    34     ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=0;
    35 }
    36 bool bfs(){
    37     memset(dep,-1,sizeof(dep));
    38     while(!q.empty()) q.pop();
    39     for(int i=1;i<=n;++i) cur[i]=head[i];
    40     dep[s]=0,q.push(s);
    41     while(!q.empty()){
    42         int u=q.front();q.pop();
    43         for(int i=head[u];i;i=Next[i]){
    44             int v=ver[i];
    45             if(dep[v]<0&&edge[i])
    46             dep[v]=dep[u]+1,q.push(v);
    47         }
    48     }
    49     if(~dep[t]) return true;
    50     return false; 
    51 }
    52 int dfs(int u,int limit){
    53     if(!limit||u==t) return limit;
    54     int flow=0,f;
    55     //这里遍历的时候要写cur而不是head
    56     //为了玄学的弧优化 
    57     for(int i=cur[u];i;i=Next[i]){
    58         cur[u]=i;int v=ver[i];
    59         if(dep[v]==dep[u]+1&&(f=dfs(v,min(limit,edge[i])))){
    60             flow+=f,limit-=f;
    61             edge[i]-=f,edge[i^1]+=f;
    62             if(!limit) break;
    63         }
    64     }
    65     return flow;
    66 }
    67 void dinic(){
    68     while(bfs()) mxflow+=dfs(s,inf);
    69 }
    70 int main(){
    71     n=read(),m=read(),s=read(),t=read();
    72     for(int i=1;i<=m;++i){
    73         int u=read(),v=read(),e=read();
    74         add(u,v,e);
    75     }
    76     dinic();
    77     print(mxflow);
    78     Ot();
    79     return 0;
    80 }
  • 相关阅读:
    smith waterman算法
    深度复数网络 Deep Complex Networks
    生成对抗网络 Generative Adversarial Networks
    dl +rec
    python 后台运行命令
    conversion vs recommendation
    激活pycharm
    keras 自定义 custom 函数
    keras 保存训练的最佳模型
    python memory-management
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/9496199.html
Copyright © 2020-2023  润新知