Poj 1087 a plug for Unix (max stream, EK algorithm)

Source: Internet
Author: User

Question: Reading is disgusting ···. That is to say, there are now n Different jacks, m different appliances, and K adapters. (The adapter is equivalent to an intermediate socket, for example, an adapter is (x, y ). There is an electrical appliance that must be inserted with an X Jack, but now there is only one y Jack, then it can be connected through an adapter ). In addition, you must note that the number of adapters is provided, and the number of each adapter is unlimited.
Question: the general idea of creating a graph. S --> Electrical Appliance --> adapter --> Jack --> T

# Include <queue> # include <cstring> # include <iostream> using namespace STD; # define n 101 # define INF 9999 char Recep [N] [25]; char device [N] [25]; struct item {char in [25]; Char out [25];} adapt [N]; bool vis [N * 3]; int pre [N * 3]; int CAP [N * 3] [N * 3]; int flow [N * 3] [N * 3]; int n, m, k; int S, T, Res; void build_map () {S = 0; t = n + M + k + 1; memset (Cap, 0, sizeof (CAP )); int I, j; for (I = 1; I <= m; I ++) {CAP [s] [I] = 1; // s to power For (j = 1; j <= N; j ++) // electrical appliance to Jack if (! Strcmp (device [I], Recep [J]) CAP [I] [J + M + k] = 1; for (j = 1; j <= K; j ++) // electrical appliance to adapter if (! Strcmp (device [I], adapt [J]. in) CAP [I] [J + M] = 1 ;}for (I = 1; I <= K; I ++) {for (j = 1; j <= K; j ++) // if (I! = J &&! Strcmp (adapt [I]. out, adapt [J]. in) CAP [I + M] [J + M] = inf; // The number is not limited, so it is inffor (j = 1; j <= N; j ++) // adapter to Jack if (! Strcmp (adapt [I]. out, Recep [J]) CAP [I + M] [J + M + k] = 1 ;}for (I = 1; I <= N; I ++) // Jack to TCAP [I + M + k] [T] = 1;} bool find_path () // BFS find augmented path {memset (PRE,-1, sizeof (-1); memset (VIS, 0, sizeof (VIS); queue <int> que; vis [s] = 1; que. push (s); While (! Que. empty () {int u = que. front (); que. pop (); For (INT v = T; V> = s; V --) {If (! Vis [v] & Cap [u] [v]> flow [u] [v]) {vis [v] = 1; Pre [v] = u; if (V = T) return true; que. push (v) ;}}return false;} int max_flow () {res = 0; memset (flow, 0, sizeof (flow); While (1) {If (! Find_path () break; int TT = T; int minflow = inf; while (pre [TT]! =-1) {If (minflow> CAP [pre [TT] [TT]-flow [pre [TT] [TT]) minflow = CAP [pre [TT] [TT]-flow [pre [TT] [TT]; TT = pre [TT];} TT = T; while (pre [TT]! =-1) {flow [pre [TT] [TT] + = minflow; flow [TT] [pre [TT]-= minflow; tt = pre [TT];} res + = minflow;} return res;} int main () {int I; char name [25]; scanf ("% d ", & N); for (I = 1; I <= N; I ++) scanf ("% s", Recep [I]); scanf ("% d ", & M); for (I = 1; I <= m; I ++) scanf ("% S % s", name, device [I]); scanf ("% d", & K); for (I = 1; I <= K; I ++) scanf ("% S % s", adapt [I]. in, adapt [I]. out); build_map (); printf ("% d \ n", M-max_flow (); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.