• 网络流Dinic--模板


      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 #include <cassert>
     21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     24 //******************
     25 int abss(int a);
     26 int lowbit(int n);
     27 int Del_bit_1(int n);
     28 int maxx(int a,int b);
     29 int minn(int a,int b);
     30 double fabss(double a);
     31 void swapp(int &a,int &b);
     32 clock_t __STRAT,__END;
     33 double __TOTALTIME;
     34 void _MS(){__STRAT=clock();}
     35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
     36 //***********************
     37 #define rint register int
     38 #define fo(a,b,c) for(rint a=b;a<=c;++a)
     39 #define fr(a,b,c) for(rint a=b;a>=c;--a)
     40 #define mem(a,b) memset(a,b,sizeof(a))
     41 #define pr printf
     42 #define sc scanf
     43 #define ls rt<<1
     44 #define rs rt<<1|1
     45 typedef vector<int> VI;
     46 typedef long long ll;
     47 const double E=2.718281828;
     48 const double PI=acos(-1.0);
     49 //const ll INF=(1LL<<60);
     50 const int inf=(1<<30);
     51 const double ESP=1e-9;
     52 const int mod=(int)1e9+7;
     53 const int N=(int)2e5+10;
     54 
     55 class DINIC
     56 {
     57 public:
     58 //    const int MAXN=10004,MAXWAY=100005;
     59     int n,way,max_flow,deep[N];
     60     int tot,head[N],cur[N];
     61     struct EDGE{
     62         int to,next;
     63         int dis;
     64     }edge[N];
     65     void Init(int n)
     66     {
     67         tot=-1;//因为加反向边要^1,所以要从0开始;
     68         for(int i=0;i<=n;++i)
     69             head[i]=-1;
     70     }
     71     void add(int from,int to,int cost,bool flag)
     72     {
     73         ++tot;
     74         edge[tot].to=to;
     75         edge[tot].dis=0;
     76         if(flag)edge[tot].dis=cost;
     77         edge[tot].next=head[from];
     78         head[from]=tot;
     79     }
     80     queue<int>q;
     81     bool bfs(int s,int t)
     82     {
     83         for(int i=1;i<=n;++i)
     84             deep[i]=inf;
     85         while(!q.empty())q.pop();
     86         for(int i=1;i<=n;++i)cur[i]=head[i];
     87         deep[s]=0;
     88         q.push(s);
     89 
     90         while(!q.empty())
     91         {
     92             int now=q.front();q.pop();
     93             for(int i=head[now];i!=-1;i=edge[i].next)
     94             {
     95                 if(deep[edge[i].to]==inf&&edge[i].dis)
     96                 {
     97                     deep[edge[i].to]=deep[now]+1;
     98                     q.push(edge[i].to);
     99                 }
    100             }
    101         }
    102         return deep[t]<inf;
    103     }
    104     int dfs(int now,int t,int limit)
    105     {
    106         if(!limit||now==t)return limit;
    107         int flow=0,f;
    108         for(int i=cur[now];i!=-1;i=edge[i].next)
    109         {
    110             cur[now]=i;
    111             if(deep[edge[i].to]==deep[now]+1&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
    112             {
    113                 flow+=f;
    114                 limit-=f;
    115                 edge[i].dis-=f;
    116                 edge[i^1].dis+=f;
    117                 if(!limit)break;
    118             }
    119         }
    120         return flow;
    121     }
    122     void Dinic(int s,int t)
    123     {
    124         while(bfs(s,t))
    125             max_flow+=dfs(s,t,inf);
    126     }
    127 }G;
    128 
    129 int main()
    130 {
    131     int S,T;
    132     sc("%d%d%d%d",&G.n,&G.way,&S,&T);
    133     G.Init(G.n);
    134     for(int i=1;i<=G.way;++i)
    135     {
    136         int u,v,cost;
    137         sc("%d%d%d",&u,&v,&cost);
    138         G.add(u,v,cost,1);
    139         G.add(v,u,cost,0);
    140     }
    141     G.Dinic(S,T);
    142     pr("%d
    ",G.max_flow);
    143     return 0;
    144 }
    145 
    146 /**************************************************************************************/
    147 
    148 int maxx(int a,int b)
    149 {
    150     return a>b?a:b;
    151 }
    152 
    153 void swapp(int &a,int &b)
    154 {
    155     a^=b^=a^=b;
    156 }
    157 
    158 int lowbit(int n)
    159 {
    160     return n&(-n);
    161 }
    162 
    163 int Del_bit_1(int n)
    164 {
    165     return n&(n-1);
    166 }
    167 
    168 int abss(int a)
    169 {
    170     return a>0?a:-a;
    171 }
    172 
    173 double fabss(double a)
    174 {
    175     return a>0?a:-a;
    176 }
    177 
    178 int minn(int a,int b)
    179 {
    180     return a<b?a:b;
    181 }
  • 相关阅读:
    快速排序理论---不含源码
    归并排序理论---不含源码
    希尔排序(shell)理论---不含源码
    Visual C++中error spawning cl.exe解决办法
    数据库的基础知识点---1
    冒泡排序的基础知识部分(不含源码)
    在虚拟机的Linux系统下安装wineqq
    数据变量的别名
    void*和void类型
    变量的作用域和连接性
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11741878.html
Copyright © 2020-2023  润新知