BotByte

Dinic_maxflow.cpp

May 3rd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. /*
  2.     Algorithm : Dinic's Algorithm for Maximum Flow
  3.     Complexity : V^2*E
  4.     Problem : LightOJ - 1153
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define INF 2000000000
  12. const int MAX_E=20005;
  13. const int MAX_V=505;
  14. int ver[MAX_E],cap[MAX_E],nx[MAX_E],last[MAX_V],ds[MAX_V],st[MAX_V],now[MAX_V],edge_count,S,T;
  15.  
  16. inline void reset()
  17. {
  18.     memset(nx,-1,sizeof(nx));
  19.     memset(last,-1,sizeof(last));
  20.     edge_count=0;
  21. }
  22.  
  23. inline void addedge(const int v,const int w,const int capacity,const int reverse_capacity)
  24. {
  25.     ver[edge_count]=w; cap[edge_count]=capacity; nx[edge_count]=last[v]; last[v]=edge_count++;
  26.     ver[edge_count]=v; cap[edge_count]=reverse_capacity; nx[edge_count]=last[w]; last[w]=edge_count++;
  27. }
  28.  
  29. inline bool bfs()
  30. {
  31.     memset(ds,-1,sizeof(ds));
  32.     int a,b;
  33.     a=b=0;
  34.     st[0]=T;
  35.     ds[T]=0;
  36.     while (a<=b)
  37.     {
  38.         int v=st[a++];
  39.         for (int w=last[v];w>=0;w=nx[w])
  40.         {
  41.             if (cap[w^1]>0 && ds[ver[w]]==-1)
  42.             {
  43.                 st[++b]=ver[w];
  44.                 ds[ver[w]]=ds[v]+1;
  45.             }
  46.         }
  47.     }
  48.     return ds[S]>=0;
  49. }
  50.  
  51. int dfs(int v,int cur)
  52. {
  53.     if (v==T) return cur;
  54.     for (int &w=now[v];w>=0;w=nx[w])
  55.     {
  56.         if (cap[w]>0 && ds[ver[w]]==ds[v]-1)
  57.         {
  58.             int d=dfs(ver[w],min(cur,cap[w]));
  59.             if (d)
  60.             {
  61.                 cap[w]-=d;
  62.                 cap[w^1]+=d;
  63.                 return d;
  64.             }
  65.         }
  66.     }
  67.     return 0;
  68. }
  69.  
  70. inline int dinic()
  71. {
  72.     int res=0;
  73.     while (bfs())
  74.     {
  75.         for (int i=0;i<MAX_V;i++) now[i]=last[i];
  76.         while (1)
  77.         {
  78.             int tf=dfs(S,INF);
  79.             res+=tf;
  80.             if (!tf) break;
  81.         }
  82.     }
  83.     return res;
  84. }
  85.  
  86. int main()
  87. {
  88.     //freopen("input.txt","r",stdin);
  89.     int cases;
  90.     scanf("%d", &cases);
  91.     int caseno = 0;
  92.     while(cases--){
  93.         reset();
  94.         int node, edge;
  95.         scanf("%d", &node);
  96.         scanf("%d %d %d", &S, &T, &edge);
  97.         for(int i=0; i<edge; i++){
  98.             int u, v, c;
  99.             scanf("%d %d %d", &u, &v, &c);
  100.             addedge(u, v, c, 0);
  101.             addedge(v, u, c, 0); // For this problem, Bidirectional Graph
  102.         }
  103.         printf("Case %d: %d\n", ++caseno, dinic());
  104.     }
  105. }
  106.  
  107. /*
  108.     4
  109.     4
  110.     1 4 2
  111.     4 1 232
  112.     1 4 490
  113.     3
  114.     1 2 2
  115.     3 2 188
  116.     2 1 100
  117.     7
  118.     2 6 6
  119.     3 6 127
  120.     3 4 515
  121.     4 6 114
  122.     2 3 123
  123.     7 1 866
  124.     4 7 588
  125.     3
  126.     3 1 7
  127.     1 3 763
  128.     1 2 883
  129.     3 1 383
  130.     1 2 641
  131.     2 1 411
  132.     2 3 229
  133.     2 1 345
  134.  
  135.     Case 1: 722
  136.     Case 2: 100
  137.     Case 3: 123
  138.     Case 4: 1375
  139. */
Advertisement
Add Comment
Please, Sign In to add comment