From f355ee266c82009056201b841d58b3a7ce183bd6 Mon Sep 17 00:00:00 2001 From: Dev-XYS Date: Sat, 11 Feb 2017 20:19:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8ISAP=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BA=8C=E5=88=86=E5=9B=BE=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=EF=BC=88=E9=99=84UOJ=20-=2078=20=E4=BA=8C=E5=88=86=E5=9B=BE?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=8C=B9=E9=85=8D=E4=BB=A3=E7=A0=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ing(Improved-Shortest-Augmenting-Path).cpp | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 Bigraph-Matching(Improved-Shortest-Augmenting-Path).cpp diff --git a/Bigraph-Matching(Improved-Shortest-Augmenting-Path).cpp b/Bigraph-Matching(Improved-Shortest-Augmenting-Path).cpp new file mode 100644 index 0000000..9f1bac0 --- /dev/null +++ b/Bigraph-Matching(Improved-Shortest-Augmenting-Path).cpp @@ -0,0 +1,121 @@ +#include + +#define INF 1000000000 + +using namespace std; + +struct vertex +{ + int first, dis; +}V[1010]; + +struct edge +{ + int endp, next, flow; +}E[502010]; + +int nl, nr, iec, ec = 2, src = 1005, sink = 1006, gap[1010]; + +inline int min(int x, int y) +{ + return x < y ? x : y; +} + +void init() +{ + gap[0] = nl + nr + 2; +} + +void add_edge(int u, int v, int f) +{ + E[ec].next = V[u].first; + V[u].first = ec; + E[ec].endp = v; + E[ec].flow = f; + ec++; +} + +int isap(int u, int curf) +{ + if (u == sink) + { + return curf; + } + int totalf = 0, mindis = nl + nr + 2; + for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next) + { + if (E[cur].flow > 0) + { + if (V[u].dis == V[E[cur].endp].dis + 1) + { + int f = isap(E[cur].endp, min(curf - totalf, E[cur].flow)); + E[cur].flow -= f; + E[cur ^ 1].flow += f; + totalf += f; + } + if (V[E[cur].endp].dis < mindis) + { + mindis = V[E[cur].endp].dis; + } + } + } + if (totalf == 0) + { + if (--gap[V[u].dis] == 0) V[src].dis = nl + nr + 2; + V[u].dis = mindis + 1; + gap[V[u].dis]++; + } + return totalf; +} + +int max_flow() +{ + int res = 0; + while (V[src].dis < nl + nr + 2) + { + res += isap(src, INF); + } + return res; +} + +int main() +{ + int u, v; + scanf("%d%d%d", &nl, &nr, &iec); + for (int i = 0; i < iec; i++) + { + scanf("%d%d", &u, &v); + add_edge(u, v + nl, 1); + add_edge(v + nl, u, 0); + } + for (int i = 1; i <= nl; i++) + { + add_edge(src, i, 1); + add_edge(i, src, 0); + } + for (int i = 1; i <= nr; i++) + { + add_edge(i + nl, sink, 1); + add_edge(sink, i + nl, 0); + } + init(); + printf("%d\n", max_flow()); + for (int i = 1; i <= nl; i++) + { + bool has = false; + for (int cur = V[i].first; cur != 0; cur = E[cur].next) + { + if (E[cur].endp != src && E[cur].flow == 0) + { + printf("%d ", E[cur].endp - nl); + has = true; + break; + } + } + if (has == false) + { + printf("0 "); + } + } + return 0; +}