Poj 1087 a plug for Unix (network stream-maximum stream (maximum binary matching ))

Source: Internet
Author: User

// Network stream-use the largest stream in the diagram for big match <br/> // connect the device to the source point <br/> // socket (plug) connect to the sink <br/> // The adapter is connected to the socket, and the weight is INF <br/> // converts the largest matching problem diagram to the largest flow problem, get the answer by subtracting the largest stream from the aggregate stream of the sink point <br/> # include <iostream> <br/> # include <queue> <br/> # include <map> <br/> # include <cstring> <br/> # include <string> <br/> # define INF 2000000000 <br/> using namespace STD; <br/> int flow [505] [505], Cap [505] [505]; <br/> int rflow [505], pre [505]; <br/> int U, V, C, maxflow, St, Ed, n, m, K; <Br/> int node; <br/> int main () <br/> {<br/> // freopen ("in.txt", "r", stdin ); <br/> maxflow = 0; <br/> node = 1; // The node number, the electric appliance and plug are numbered accordingly. <br/> ST = 0; <br/> ED = 1; <br/> string plug, device, adapter; <br/> Map <string, int> m; // map is used to associate node numbers of various electrical appliances and adapters. <br/> CIN> N; <br/> memset (flow, 0, sizeof (flow); <br/> memset (Cap, 0, sizeof (CAP )); <br/> for (INT I = 0; I <n; ++ I) <br/>{< br/> CIN> plug; <br/> M [plug] = ++ n Ode; <br/> CAP [node] [ed] = 1; <br/>}< br/> CIN> m; <br/> for (INT I = 0; I <m; ++ I) <br/>{< br/> CIN> device> plug; <br/> M [device] = ++ node; <br/> CAP [st] [M [device] = 1; <br/> If (! M. count (plug) <br/>{< br/> M [plug] = ++ node; <br/>}< br/> CAP [M [device] [M [plug] = 1; <br/>}< br/> CIN> K; <br/> for (INT I = 0; I <K; ++ I) <br/>{< br/> CIN> adapter> plug; <br/> If (! M. Count (adapter) <br/>{< br/> M [adapter] =++ node; <br/>}< br/> If (! M. count (plug) <br/>{< br/> M [plug] = ++ node; <br/>}< br/> CAP [M [adapter] [M [plug] = inf; // note that, he also said that the number of adapters is infinite, so the weight for constructing the adapter to the plug should be INF <br/>}< br/> (;;) // maximum stream template <br/>{< br/> queue <int> q; <br/> memset (rflow, 0, sizeof (rflow )); <br/> rflow [st] = inf; <br/> q. push (ST); <br/> while (! Q. empty () <br/>{< br/> U = Q. front (); <br/> q. pop (); <br/> for (V = 0; v <= node; ++ v) <br/>{< br/> If (! Rflow [v] & Cap [u] [v]> flow [u] [v]) <br/> {<br/> rflow [v] = min (rflow [u], Cap [u] [v]-flow [u] [v]); <br/> pre [v] = u; <br/> q. push (V); <br/>}< br/> If (rflow [ed] = 0) break; <br/> for (u = Ed; u! = ST; u = pre [u]) <br/>{< br/> flow [pre [u] [u] + = rflow [ed]; // update forward traffic <br/> flow [u] [pre [u]-= rflow [ed]; // update reverse traffic <br/>}< br/> maxflow + = rflow [ed]; <br/>}< br/> printf ("% d/N ", m-maxflow); // The sum of the traffic to the sink point minus the maximum stream is the answer <br/> return 0; <br/>} 

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.