zoukankan      html  css  js  c++  java
  • 最大流isap模板

    isap+bfs初始化+栈优化,点的编号从0开始:

      1 const int MAXN = 100010;
      2 const int MAXM = 400010;
      3 const int INF = 0x3f3f3f3f;
      4 struct Edge 
      5 {
      6     int to, next, cap, flow;
      7 }edge[MAXM];
      8 int tol;
      9 int head[MAXN];
     10 int gap[MAXN], dep[MAXN], cur[MAXN];
     11 void init() 
     12 {
     13     tol = 0;
     14     memset(head, -1, sizeof(head));
     15 }
     16 void addedge(int u, int v, int w, int rw = 0) 
     17 {
     18     edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
     19     edge[tol].next = head[u]; head[u] = tol++;
     20     edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
     21     edge[tol].next = head[v]; head[v] = tol++;
     22 }
     23 int Q[MAXN];
     24 void BFS(int start, int end) 
     25 {
     26     memset(dep, -1, sizeof(dep));
     27     memset(gap, 0, sizeof(gap));
     28     gap[0] = 1;
     29     int front = 0, rear = 0;
     30     dep[end] = 0;
     31     Q[rear++] = end;
     32     while(front != end) {
     33         int u = Q[front++];
     34         for(int i = head[u]; i != -1; i = edge[i].next) {
     35             int v = edge[i].to;
     36             if(dep[v] != -1) continue;
     37             Q[rear++] = v;
     38             dep[v] = dep[u] + 1;
     39             gap[dep[v]]++;
     40         }
     41     }
     42 }
     43 int S[MAXN];
     44 int sap(int start, int end, int N) {  //start是源点编号,end是汇点编号,N是点的总数,返回最大流
     45     BFS(start, end);
     46     memcpy(cur, head, sizeof(head));
     47     int top = 0;
     48     int u = start;
     49     int ans = 0;
     50     while(dep[start] < N) 
     51     {
     52         if(u == end) 
     53         {
     54             int Min = INF;
     55             int inser;
     56             for(int i = 0; i < top; i++)
     57                 if(Min > edge[S[i]].cap - edge[S[i]].flow) {
     58                     Min = edge[S[i]].cap - edge[S[i]].flow;
     59                     inser = i;
     60                 }
     61             for(int i = 0; i < top; i++) 
     62             {
     63                 edge[S[i]].flow += Min;
     64                 edge[S[i]^1].flow -= Min;
     65             }
     66             ans += Min;
     67             top = inser;
     68             u = edge[S[top]^1].to;
     69             continue;
     70         }
     71         bool flag = false;
     72         int v;
     73         for(int i = cur[u]; i != -1; i = edge[i].next) 
     74         {
     75             v = edge[i].to;
     76             if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u])                          
     77                 flag = true;
     78                 cur[u] = i;
     79                 break;
     80             }
     81         }
     82         if(flag) 
     83         {
     84             S[top++] = cur[u];
     85             u = v;
     86             continue;
     87         }
     88         int Min = N;
     89         for(int i = head[u]; i != -1; i = edge[i].next)
     90             if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) 
     91             {
     92                 Min = dep[edge[i].to];
     93                 cur[u] = i;
     94             }
     95         gap[dep[u]]--;
     96         if(!gap[dep[u]]) return ans;
     97         dep[u] = Min + 1;
     98         gap[dep[u]]++;
     99         if(u != start) u = edge[S[--top]^1].to;
    100     }
    101     return ans;
    102 }         
  • 相关阅读:
    Oracle
    CCF 201609-5 祭坛
    prometheus同时执行多个查询
    压力测试 Apache ab
    kubernetes reference
    python 深拷贝
    [ argo workflow ]
    django orm 改动数据库中已存在的表(添加、删除、修改表字段)migrations
    内存压力测试命令
    django 未成功初始化自定义表单
  • 原文地址:https://www.cnblogs.com/yaoyueduzhen/p/5020377.html
Copyright © 2011-2022 走看看