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

poj3281 Dining

时间:2017-12-02 16:15:59      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:clu   iostream   struct   i++   iter   name   std   front   while   

思路:

最大流。

实现:

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 
 7 const int MAXV = 403, INF = 0x3f3f3f3f;
 8 struct edge
 9 {
10     int to, cap, rev;
11     edge(int t, int c, int r) { to = t; cap = c; rev = r; }
12 };
13 int level[MAXV];
14 int iter[MAXV];
15 vector<edge> G[MAXV];
16 int n, f, d;
17 void add_edge(int from, int to, int cap)
18 {
19     edge e(to, cap, G[to].size());
20     G[from].push_back(e);
21     edge er(from, 0, G[from].size() - 1);
22     G[to].push_back(er);
23 }
24 void bfs(int s)
25 {
26     memset(level, -1, sizeof level);
27     queue<int> q;
28     level[s] = 0;
29     q.push(s);
30     while (!q.empty())
31     {
32         int v = q.front(); q.pop();
33         for (int i = 0; i < G[v].size(); i++)
34         {
35             edge& e = G[v][i];
36             if (e.cap > 0 && level[e.to] < 0)
37             {
38                 level[e.to] = level[v] + 1;
39                 q.push(e.to);
40             }
41         }
42     }
43 }
44 int dfs(int v, int t, int f)
45 {
46     if (v == t) return f;
47     for (int& i = iter[v]; i < G[v].size(); i++)
48     {
49         edge& e = G[v][i];
50         if (e.cap > 0 && level[v] < level[e.to])
51         {
52             int d = dfs(e.to, t, min(f, e.cap));
53             if (d > 0)
54             {
55                 e.cap -= d;
56                 G[e.to][e.rev].cap += d;
57                 return d;
58             }
59         }
60     }
61     return 0;
62 }
63 int max_flow(int s, int t)
64 {
65     int flow = 0;
66     for (;;)
67     {
68         bfs(s);
69         if (level[t] < 0) return flow;
70         memset(iter, 0, sizeof iter);
71         int f = 0;
72         while ((f = dfs(s, t, INF)) > 0)
73             flow += f;
74     }
75 }
76 
77 int main()
78 {
79     cin >> n >> f >> d;
80     for (int i = 1; i <= n; i++)
81     {
82         int t1, t2, x;
83         cin >> t1 >> t2;
84         for (int j = 1; j <= t1; j++)
85         {
86             cin >> x; add_edge(x, f + i, 1);
87         }
88         for (int j = 1; j <= t2; j++)
89         {
90             cin >> x; add_edge(f + n + i, f + 2 * n + x, 1);
91         }
92     }
93     for (int i = 1; i <= f; i++) add_edge(0, i, 1);
94     for (int i = 1; i <= d; i++) add_edge(f + 2 * n + i, f + 2 * n + d + 1, 1);
95     for (int i = 1; i <= n; i++) add_edge(f + i, f + n + i, 1);
96     cout << max_flow(0, f + 2 * n + d + 1) << endl;
97     return 0;
98 }

 

poj3281 Dining

标签:clu   iostream   struct   i++   iter   name   std   front   while   

原文地址:http://www.cnblogs.com/wangyiming/p/7954202.html

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