Hdoj 3605 escape [medium maximum stream | bipartite graph multi-match]

Source: Internet
Author: User

Title: hdoj 3605 escape


Category: medium maximum stream | bipartite graph multi-match


Question: N people and M planets are given. Everyone is interested in what they want, and each planet has a capacity. Can anyone live on the planet they want?


Analysis: if the maximum stream is used, the card is very strict. After this topic is written, the MTL is handwritten, the memory is exceeded, and then the status is compressed, and then the Tel cannot be viewed later, it is not easy to change the C ++ compilation error. The VC compiler used in C ++ actually changed it to Ms.

First, the question is very obvious, and the graph creation method is also very obvious. We need to set a super-long point S and a sink point t, and the rest will not be said.

First, the first step is optimization. The State is compressed. Because there are 100000 people and there are only 10 planets, each person wants to go to a planet with a state of up to 1 <10, about 1000 points, therefore, it is a great optimization to calculate the status first, and then create a graph after compression. This topic is also very strict with the network stream data card, and should be optimized in order to use dinci or sap.


Through this question, we know that C ++ uses the VC compiling environment, so we don't need compilation error in the future.

Network Flow AC code:

# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> # include <vector> # include <queue> # include <cmath> using namespace STD; # define del (a, B) memset (a, B, sizeof (A) const int n = 1210; const int INF = 0x3f3f3f; const double ESP = 1e-9; int N, m; struct node {int from, to, Cap, flow ;}; vector <int> V [N]; int sum [N]; vector <node> E; int vis [N]; // build the layered graph int cur [N]; int min (int x, in T y) {return x> Y? Y: X;} void add_node (int from, int to, int cap) {node tmp1, tmp2; tmp1.from = from, tmp1.to = To, tmp1.cap = CAP, tmp1.flow = 0; e. push_back (tmp1); tmp2.from = To, tmp2.to = from, tmp2.cap = 0, tmp2.flow = 0; E. push_back (tmp2); int TMP = E. size (); V [from]. push_back (tmp-2); V [to]. push_back (tmp-1);} bool BFS (int s, int t) {del (VIS,-1); queue <int> q; q. push (s); vis [s] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); int I; for (I = 0; I <V [X]. size (); I ++) {node TMP = E [V [x] [I]; If (vis [TMP. to] <0 & TMP. cap> TMP. flow) // The second condition ensures {vis [TMP. to] = vis [x] + 1; q. push (TMP. to) ;}}} if (vis [T]> 0) return true; return false;} int DFS (int o, int F, int T) {If (O = T | f = 0) // returns F; int A = 0, ANS = 0; Int & I = cur [O]; for (; I <V [O]. size (); I ++) // note the preceding '&', which is an important optimization {node & TMP = E [V [O] [I]; If (vis [TMP. to] = (vis [O] + 1) & (A = DFS (TMP. to, min (F, TMP. cap-tmp.flow), t)> 0) {TMP. flow + = A; E [V [O] [I] ^ 1]. flow-= A; // save graph mode ans + = A; F-= A; If (F = 0) // note break optimization;} return ans; // optimization} int dinci (int s, int t) {int ans = 0; while (BFS (S, T) {del (cur, 0 ); int TM = DFS (S, INF, T); ans + = TM;} return ans;} int main () {int n, m; while (~ Scanf ("% d", & N, & M) {int I, j; memset (sum, 0, sizeof (SUM); int S = 0, x, t = m + (1 <m) + 3, NN = (1 <m) + 2; for (I = 1; I <= N; I ++) {int TMP = 0; For (j = 0; j <m; j ++) {scanf ("% d", & X); If (X) TMP + = (1 <j);} sum [TMP] ++;} for (I = 0; I <(1 <m); I ++) {If (sum [I]) {add_node (S, I + 1, sum [I]); For (j = 0; j <m; j ++) {if (I & (1 <j) add_node (I + 1, NN + J, sum [I]) ;}}for (I = 0; I <m; I ++) {scanf ("% d", & X); add_node (Nn + I, t, x);} int ans = dinci (S, t); // printf ("% d \ n", ANS); If (ANS = N) printf ("Yes \ n "); else printf ("NO \ n"); for (I = 0; I <= T; I ++) V [I]. clear (); E. clear () ;}return 0 ;}


Hdoj 3605 escape [medium maximum stream | bipartite graph multi-match]

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.