zoukankan      html  css  js  c++  java
  • 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 #define INF 2147483647
     7 #define MAXN 201
     8 #define MAXM 1501
     9 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];
    10 int d[MAXN],cur[MAXN];
    11 queue<int>q;
    12 int m,S,T,W;
    13 char U[1],V[1];
    14 void Init_Dinic(){memset(first,-1,sizeof(first)); en=0; S='A'; T='Z';}
    15 void AddEdge(const int &U,const int &V,const int &W)
    16 {v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;
    17 v[en]=U; next[en]=first[V]; first[V]=en++;}
    18 bool bfs()
    19 {
    20     memset(d,-1,sizeof(d)); q.push(S); d[S]=0;
    21     while(!q.empty())
    22       {
    23         int U=q.front(); q.pop();
    24         for(int i=first[U];i!=-1;i=next[i])
    25           if(d[v[i]]==-1 && cap[i])
    26             {
    27               d[v[i]]=d[U]+1;
    28               q.push(v[i]);
    29             }
    30       }
    31     return d[T]!=-1;
    32 }
    33 int dfs(int U,int a)
    34 {
    35     if(U==T || !a) return a;
    36     int Flow=0,f;
    37     for(int &i=cur[U];i!=-1;i=next[i])
    38       if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))
    39         {
    40           cap[i]-=f; cap[i^1]+=f;
    41           Flow+=f; a-=f; if(!a) break;
    42         }
    43     if(!Flow) d[U]=-1;
    44     return Flow;
    45 }
    46 int max_flow()
    47 {
    48     int Flow=0,tmp=0;
    49     while(bfs())
    50       {
    51         memcpy(cur,first,sizeof(cur));
    52         while(tmp=dfs(S,INF)) Flow+=tmp;
    53       }
    54     return Flow;
    55 }
    56 int main()
    57 {
    58     scanf("%d",&m); Init_Dinic();
    59     for(int i=1;i<=m;++i)
    60       {
    61           scanf("%s%s%d",U,V,&W);
    62           AddEdge(U[0],V[0],W);
    63       }
    64     printf("%d
    ",max_flow());
    65     return 0;
    66 }
  • 相关阅读:
    centos7下查看端口是否被占用
    centos7通过yum安装redis
    centos7下mysql远程连接
    centos7通过yum安装JDK1.8
    Jwt系列2:使用
    Jwt系列1:简介
    Python基础语法
    排序
    利用MultipartFile来进行文件上传
    服务器端跳转和客户端跳转
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/4149361.html
Copyright © 2011-2022 走看看