-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path684.cpp
More file actions
134 lines (118 loc) · 2.45 KB
/
Copy path684.cpp
File metadata and controls
134 lines (118 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#define MAXN 2100
#define INF 2147483647
using namespace std;
struct Edge {
int u, v;
int cap, flow;
Edge() {}
Edge(int u, int v, int cap, int flow) :u(u), v(v), cap(cap), flow(flow) {}
};
vector<Edge> edges;
vector<int> graph[MAXN];
int level[MAXN];
int cur[MAXN];
int src, sink;
bool counting = false;
int fireCnt;
bool isFired[MAXN];
void init() {
edges.clear();
for (int i = 0; i < MAXN; i++) {
graph[i].clear();
}
}
void addEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
graph[from].push_back(m - 2);
graph[to].push_back(m - 1);
}
bool genLevel() {
bool reached = false;
memset(level, -1, sizeof(int)*MAXN);
level[src] = 0;
queue<int> q;
q.push(src);
while (!q.empty()) {
int current = q.front();
if (current == sink) {
reached = true;
break;
}
q.pop();
for (int i = 0; i < graph[current].size(); i++) {
Edge ed = edges[graph[current][i]];
if (level[ed.v] < 0 && ed.flow < ed.cap) {
level[ed.v] = level[current] + 1;
q.push(ed.v);
}
}
}
return reached;
}
int findPath(int pos, int limit) {
if (pos == sink || limit == 0) {
return limit;
}
int totalAdd = 0;
int addFlow;
for (int &i = cur[pos]; i < graph[pos].size(); i++) {
Edge &ed = edges[graph[pos][i]];
if (level[pos] + 1 == level[ed.v] && ed.cap > ed.flow) {
addFlow = findPath(ed.v, limit < (ed.cap - ed.flow) ? limit : (ed.cap - ed.flow));
if (addFlow > 0) {
ed.flow += addFlow;
edges[graph[pos][i] ^ 1].flow -= addFlow;
totalAdd += addFlow;
limit -= addFlow;
if (limit == 0) {
break;
}
}
}
}
return totalAdd;
}
int main() {
int n, m, k;
while (cin >> n >> m >> k) {
init();
src = n + m + 1;
sink = n + m + 2;
addEdge(src, 0, 0);
for (int i = 1; i < n; i++) {
addEdge(src, i, 1);
}
addEdge(n, sink, 0);
for (int i = 1; i < m; i++) {
addEdge(n + i, sink, 1);
}
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
addEdge(x, n + y, INF);
}
int sum = 0;
while (genLevel()) {
memset(cur, 0, sizeof(int) * MAXN);
sum += findPath(src, INF);
}
cout << sum << endl;
/*
for (int i = 0; i < graph[src].size(); i++) {
cout << edges[graph[src][i]].flow << " ";
}
cout << endl;
for (int i = n; i < n + m; i++) {
cout << edges[graph[i][0]].flow << " ";
}
cout << endl;*/
}
system("pause");
return 0;
}