码迷,mamicode.com
首页 > 其他好文 > 详细

最大流

时间:2015-07-27 12:41:21      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

poj 3281

学会建图啊。。 啊。。 啊。。

S(0)-食物(2n+i)-牛1(i)-牛2(n+i)-饮料(2n+F+i)-T(2n+F+D+1)

技术分享
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<set>
  7 #include<map>
  8 #include<stack>
  9 #include<vector>
 10 #include<queue>
 11 #include<string>
 12 #include<sstream>
 13 #define eps 1e-9
 14 #define ALL(x) x.begin(),x.end()
 15 #define INS(x) inserter(x,x.begin())
 16 #define FOR(i,j,k) for(int i=j;i<=k;i++)
 17 #define MAXN 1005
 18 #define MAXM 30005
 19 using namespace std;
 20 typedef long long LL;
 21 int i, j, k, n, m, x, y, T, ans, big, cas, F, D, S, f, d, id;
 22 bool flag;
 23 
 24 const int inf = 0x3f3f3f3f;
 25 struct edgenode
 26 {
 27     int from, to, next;
 28     int cap;
 29 }edge[MAXM];
 30 int Edge, head[MAXN], ps[MAXN], dep[MAXN];
 31 
 32 void add_edge(int x, int y, int c)
 33 {
 34     edge[Edge].from = x;
 35     edge[Edge].to = y;
 36     edge[Edge].cap = c;
 37     edge[Edge].next = head[x];
 38     head[x] = Edge++;
 39 
 40     edge[Edge].from = y;
 41     edge[Edge].to = x;
 42     edge[Edge].cap = 0;
 43     edge[Edge].next = head[y];
 44     head[y] = Edge++;
 45 }
 46 
 47 int dinic(int n, int s, int t)
 48 {
 49     int tr, flow = 0;
 50     int i, j, k, l, r, top;
 51     while (1){
 52         memset(dep, -1, (n + 1)*sizeof(int));
 53         for (l = dep[ps[0] = s] = 0, r = 1; l != r;)//BFS部分,将给定图分层
 54         {
 55             for (i = ps[l++], j = head[i]; j != -1; j = edge[j].next)
 56             {
 57                 if (edge[j].cap&&-1 == dep[k = edge[j].to])
 58                 {
 59                     dep[k] = dep[i] + 1; ps[r++] = k;
 60                     if (k == t)
 61                     {
 62                         l = r;
 63                         break;
 64                     }
 65                 }
 66             }
 67         }
 68         if (dep[t] == -1)break;
 69 
 70         for (i = s, top = 0;;)//DFS部分
 71         {
 72             if (i == t)//当前点就是汇点时
 73             {
 74                 for (k = 0, tr = inf; k<top; ++k)
 75                     if (edge[ps[k]].cap<tr)tr = edge[ps[l = k]].cap;
 76 
 77                 for (k = 0; k<top; ++k)
 78                     edge[ps[k]].cap -= tr, edge[ps[k] ^ 1].cap += tr;
 79 
 80                 flow += tr;
 81                 i = edge[ps[top = l]].from;
 82             }
 83 
 84             for (j = head[i]; j != -1; j = edge[j].next)//找当前点所指向的点
 85                 if (edge[j].cap&&dep[i] + 1 == dep[edge[j].to]) break;
 86 
 87             if (j != -1)
 88             {
 89                 ps[top++] = j;//当前点有所指向的点,把这个点加入栈中
 90                 i = edge[j].to;
 91             }
 92             else
 93             {
 94                 if (!top) break;//当前点没有指向的点,回溯
 95                 dep[i] = -1;
 96                 i = edge[ps[--top]].from;
 97             }
 98         }
 99     }
100     return flow;
101 }
102 
103 
104 int main()
105 {
106     memset(head, -1, sizeof(head)); Edge = 0;
107     scanf("%d%d%d", &n, &F, &D);
108     S = 0;
109     T = n + n + F + D + 1;
110     for (i = 1; i <= F; i++)
111     {
112         add_edge(S, 2 * n + i, 1);
113     }
114     for (i = 1; i <= D; i++)
115     {
116         add_edge(2 * n + F + i, T, 1);
117     }
118 
119     for (i = 1; i <= n; i++)
120     {
121         scanf("%d%d", &f, &d);
122         add_edge(i, n + i, 1);
123         for (j = 1; j <= f; j++)
124         {
125             scanf("%d", &id);
126             id = 2 * n + id;
127             add_edge(id, i, 1);
128         }
129         for (j = 1; j <= d; j++)
130         {
131             scanf("%d", &id);
132             id = 2 * n + F + id;
133             add_edge(n + i, id, 1);
134         }
135     }
136     printf("%d\n", dinic(T + 1, S, T));
137     return 0;
138 }
View Code

 

最大流

标签:

原文地址:http://www.cnblogs.com/usedrosee/p/4679654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!