hdu2063 过山车

hdu2063 过山车

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2005, inf = 0x3f3f3f;
 4 struct Edge {
 5     int from, to, cap, flow;
 6 };
 7 
 8 struct Dinic {
 9     int n, m, s, t;
10     vector<Edge> edges;
11     vector<int> G[maxn];
12     bool vis[maxn];
13     int d[maxn];
14     int cur[maxn];
15 
16     void init(int n) {
17         this->n = n;
18         for (int i = 1; i <= n; ++i) {
19             G[i].clear();
20         }
21         edges.clear();
22     }
23     void AddEdge(int from, int to, int cap) {
24         edges.push_back((Edge){from, to, cap, 0});
25         edges.push_back((Edge){to, from, 0, 0});
26         m = edges.size();
27         G[from].push_back(m-2);
28         G[to].push_back(m-1);
29     }
30     bool bfs() {
31         memset(vis, 0, sizeof(vis));
32         queue<int> que;
33         que.push(s);
34         d[s] = 0;
35         vis[s] = true;
36         while (!que.empty()) {
37             int x = que.front(); que.pop();
38             for (int i = 0; i < G[x].size(); ++i) {
39                 Edge& e = edges[G[x][i]];
40                 if (!vis[e.to] && e.cap > e.flow) {
41                     vis[e.to] = true;
42                     d[e.to] = d[x] + 1;
43                     que.push(e.to);
44                 }
45             }
46         }
47         return vis[t];
48     }
49     int dfs(int x, int a) {
50         if (x == t || a == 0) return a;
51         int flow = 0, f;
52         for (int& i = cur[x]; i < G[x].size(); ++i) {
53             Edge& e = edges[G[x][i]];
54             if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0) {
55                 e.flow += f;
56                 edges[G[x][i]^1].flow -= f;
57                 flow += f;
58                 a -= f;
59                 if (a == 0) break;
60             }
61         }
62         return flow;
63     }
64     int maxflow(int s, int t) {
65         this->s = s; this->t = t;
66         int flow = 0;
67         while (bfs()) {
68             memset(cur,0,sizeof(cur));
69             flow += dfs(s,inf);
70         }
71         return flow;
72     }
73 }dinic;
74 int main() {
75     int e;
76     while (~scanf("%d",&e) && e) {
77         int n, m; scanf("%d%d",&n,&m);
78         dinic.init(n+m+2);
79         int s = n+m+1, t = n+m+2;
80         for (int i = 1; i <= n; ++i) {
81             dinic.AddEdge(s,i,1);
82         }
83         for (int i = 1; i <= m; ++i) {
84             dinic.AddEdge(i+n,t,1);
85         }
86         for (int i = 1; i <= e; ++i) {
87             int u, v; scanf("%d%d",&u,&v);
88             dinic.AddEdge(u,v+n,1);
89         }
90         int ans = dinic.maxflow(s,t);
91         printf("%d\n",ans);
92     }
93     return 0;
94 }

 

posted @ 2019-11-02 16:10  麻辣猪仔  阅读(123)  评论(0编辑  收藏  举报