-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMachineSchedule684.cpp
More file actions
81 lines (81 loc) · 2.13 KB
/
MachineSchedule684.cpp
File metadata and controls
81 lines (81 loc) · 2.13 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
//This is a minimum vertex cover program. Minimum vertex cover is the same as maximum bipartite matching.
#include <iostream>
#include <string.h>
#include <queue>
#include <math.h>
#include <limits.h>
using namespace std;
inline bool bfs(int **graph, int source, int sink, int *parent,int numberOfNodes ) {// bfs to return path from source to sink
bool *visited = new bool[numberOfNodes];
memset(visited, 0, sizeof(bool)*numberOfNodes);
queue <int>q;
q.push(source);
visited[source] = true;
parent[source] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v = 0; v < numberOfNodes; v++) {
if (visited[v] == false && graph[u][v] > 0) {
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
return (visited[sink]);
}
inline int edmondKarp(int **graph, int source, int sink,int numberOfNodes) {
int **residualGraph = new int*[numberOfNodes];//residual capacity of an edge
for (int i = 0; i < numberOfNodes; i++) {
residualGraph[i] = new int[numberOfNodes];
}
for (int i = 0; i < numberOfNodes; i++) {
for (int j = 0; j < numberOfNodes; j++) {
residualGraph[i][j] = graph[i][j];
}
}
int *parent=new int[numberOfNodes];
int max_flow = 0;
while (bfs(residualGraph, source, sink, parent, numberOfNodes)) {
int path_flow = INT_MAX;
for (int i = sink; i != source; i = parent[i]) {
int u = parent[i];
path_flow = min(path_flow, residualGraph[u][i]);
}
for (int i = sink; i != source; i = parent[i]) {
int u = parent[i];
residualGraph[u][i] -= path_flow;
residualGraph[i][u] += path_flow;
}
max_flow += path_flow;
}
return max_flow;
}
int main() {
int n, m, k;
while (cin >> n >> m >> k) {
//n+m is source and n+m+1 is sink
int ctr = 0;
int ctr1 = 0;
int ctr2 = 0;
int **G = new int*[(n + m) + 2];
int size = n + m + 2;
for (int i = 0; i < size; i++) {
G[i] = new int[size];
memset(G[i], 0, sizeof(int)*size);
}
for (int i = 0; i < k; i++) {
int u, v;
cin >> u >> v;
if (u == 0 || v == 0)
continue;
G[u][v+n] = 1;
G[n + m ][u] = 1;
G[v+n][n + m + 1]=1;
}
int flow = edmondKarp(G, n + m, n + m + 1, size);
cout << flow << endl;
}
return 0;
}