Network stream hdoj 4292

Source: Internet
Author: User

You can build a network flow model based on the question

From the source point to the food edge, the weight is the number of foods;

From the foods that I like to node 1 of I, the edge weight is 1.

From the I-th personal Node 1 to I-th personal Node 2, the edge weight is 1, preventing a person from selecting multiple times

From personal I Node 2 to beverage, edge weight is 1

From drinks to collection points, edge weight is the number of drinks

Build as shown in Figure

# Include <stdio. h>

# Include <string. h>
# Include <algorithm>
Using namespace STD;
# Deprecision Max 1001
Int last;
Int map [Max] [Max];

Int F, d, n;
Char STR [211];

Int que [Max], num;
Int inque [Max];
Int H [Max];

Int min (int A, int B)
{
Return a <B? A: B;
}

Int DFS (INT pre, int Minx)
{
Int;
If (pre = last)
Return Minx;
For (INT I = 0; I <= last; I ++)
{
If (Map [pre] [I]> 0 & H [I] = H [pre] + 1)
{
A = DFS (I, min (Map [pre] [I], Minx ));
If (A = 0)
Continue;
Map [pre] [I]-=;
Map [I] [pre] + =;
Return;
}
}
H [pre] =-1;
// Pay attention to the Optimization here. As long as there is no streaming in this place, the DFS does not need to search for this node before the next wide search, and mark it
Return 0;
}

Int BFS ()
{
Int I;

Num = 0;

Memset (inque, 0, sizeof (inque ));
Que [num] = 0;
Inque [0] = 1;
H [0] = 0;

Int front = 0;

Int W = 1, Index = 1;

While (front <= num)
{
If (front = W)
{
Index ++;
W = num + 1;
}
For (I = 0; I <= last; I ++)
{
If (Map [que [Front] [I] = 0)
Continue;
If (inque [I])
Continue;
Que [++ num] = I;
Inque [I] = 1;
H [I] = index;
If (I = last)
Return 1;
// Pay attention that as long as the wide search result reaches the collection point, there must be a stream increase
}
Front ++;
}
Return 0;
// If no stream is found, no streaming will be added.
}

Int maxflow ()
{
Int result = 0;
Int get;
While (BFS ())
{
While (get = DFS (0, max) // a while is missing here, leading to timeout, because there are many scenarios where no search is required.
Result + = get;
}
Return result;
}

Int main ()
{
Int I, J;
While (scanf ("% d", & N, & F, & D )! = EOF)
{
Last = F + 2 * n + D + 1;
Memset (MAP, 0, sizeof (MAP ));
// When building a graph, I thought that only one node would be enough. Later I found that this node may flow through multiple times, so I added a node and edge weight.
1 to prevent this situation.
For (I = 1; I <= f; I ++)
Scanf ("% d", & map [0] [I]);
For (I = 1; I <= D; I ++)
Scanf ("% d", & map [F + 2 * n + I] [last]);
For (I = 1; I <= N; I ++)
{
Scanf ("% s", STR + 1 );
For (j = 1; j <= f; j ++)
{
If (STR [J] = 'y ')
{
Map [J] [F + I] = 1;
}
}
}
For (I = 1; I <= N; I ++)
{
Scanf ("% s", STR + 1 );
For (j = 1; j <= D; j ++)
{
If (STR [J] = 'y ')
{
Map [F + N + I] [F + 2 * n + J] = 1;
}
}
}
For (I = 1; I <= N; I ++)
Map [F + I] [F + N + I] = 1;
Printf ("% d \ n", maxflow ());
}
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.