Poj 2594 treasure transition ation (Transfer closure + minimum path overwrite)

Source: Internet
Author: User

// The build of the passed closure (Floyd) + the minimum path overwrite <br/> // This question is different from 1422 because it is a directed graph rather than Dag, the robot can go back to another path. <br/> // the roads of two different robots may contain some same point. <br/> // For example, 1-> 2, 2-> 3, 3-> 4, 4-> 2, 2-> 5. This graph can be completed by only one robot, however, if the passing closure is not set up <br/> // directly matches based on the edge, the result is incorrect, because the path overwrites that are usually referred, the vertex can only go through once at most, and this question can go through multiple times <br/> // Therefore, you must use Floyd to re-determine the connectivity and create a passing closure, create a New Graph Based on the closure, make sure that each vertex passes through only once <br/> // so that the minimum path overwrite can be obtained using the maximum matching method of the Bipartite Graph. <br/> # include <iostream> <B R/> using namespace STD; <br/> const int max = 502; <br/> int n, m; <br/> bool a [Max] [Max]; <br/> int X, Y, vis [Max], G [Max] [Max], ymatch [Max]; <br/> void Floyd () // evaluate the passed closure and create a new graph <br/>{< br/> for (int K = 1; k <= N; ++ K) <br/> for (INT I = 1; I <= N; ++ I) <br/> for (Int J = 1; j <= N; ++ J) <br/>{< br/> if (I = J) continue; <br/> if (a [I] [k] & A [k] [J]) <br/> A [I] [J] = 1; <br/>}< br/> for (INT I = 1; I <= N; ++ I) <br/> for (Int J = 1; J <= N; ++ J) <br/> if (a [I] [J]) g [I] [J] = 1; <br/>}< br/> bool findpath (int x) // search for augmented path <br/>{< br/> for (INT y = 1; Y <= y; ++ y) <br/>{< br/> If (! Vis [y] & G [x] [Y]) <br/>{< br/> vis [y] = 1; <br/> If (ymatch [y] =-1 | findpath (ymatch [y]) // If V is not matched or V finds a new augmented path <br/>{< br/> ymatch [y] = x; <br/> return true; <br/>}< br/> return false; <br/>}< br/> int maxmatch () // returns the maximum number of matches <br/>{< br/> int ans = 0; <br/> memset (ymatch,-1, sizeof (ymatch )); // initialization <br/> for (INT x = 1; x <= x; ++ X) // note the numbers <br/> {<br/> memset (VIS, 0, sizeof (VIS); <br/> If (findpath (x )) + + ans; <br/>}< br/> return ans; <br/>}< br/> int main () <br/>{< br/> int X, Y; <br/> while (scanf ("% d", & N, & M) & N) <br/>{< br/> memset (A, 0, sizeof (a); <br/> memset (G, 0, sizeof (g )); <br/> while (M --) <br/>{< br/> scanf ("% d", & X, & Y ); <br/> A [x] [Y] = 1; <br/>}< br/> Floyd (); <br/> X = y = N; <br/> printf ("% d/N", X-maxmatch (); <br/>}< 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.