Poj (1, 1149) pigs

Source: Internet
Author: User

 

// The difficulty of this question is how to create a graph. Let R get a certain value ..
// Each customer is represented by a node.
// For the first customer in each pigsty, the edge is connected from the source point, and the capacity is the initial number of pig in the pigsty. If there are multiple edges from the source point to a customer, they can be merged into one and the capacity is added.
// For each pigsty, assume that N Customers have opened it, then for All integers I, [1, n ), from the customer I of the pigsty to the customer I + 1, the capacity is + ∞.
// Each customer has an edge from the customer to the settlement point. The capacity is the maximum quantity that each customer can buy.

 

 

 

The general idea of this question is as follows:
There are m pigtails (m ≤ 1000), and each pigsty initially has several pig heads. At first, all pig circles were
. There are N Customers (n ≤ 100) in sequence, and each customer opens the specified pigsty,
Buy some pig from it. Each customer has an upper limit on the quantity he can buy. After each customer leaves
The pig in the pigsty can be transferred to other pig in any way, and then all pig circles are re-launched.
Close. Ask how many pigs can be sold in total.

For example. There are 3 pig rings, and 3, 1, and 10 pig in the initial stage. Three
Customer, the first to open pigsty No. 1 and No. 2, a maximum of two; the second to open pigsty No. 1 and No. 3
Pigsty, buy up to 3; the third opens pigsty 2, buy up to 6. So, the best possibility
One is that the first customer buys two heads from the first lap, and then places the remaining 1 in the first lap to the second lap;
The second customer buys three orders from the third circle, and the third customer buys two orders from the second circle. Sell 2 + 3 in total
+ 2 = 7 Heads, it is hard to imagine that the network model of this problem can be constructed intuitively. Take the above.
For example, the model shown in Figure 1 can be constructed (the edge capacity without any decimal number in the figure is + ∞ ):
Three customers have three rounds of transactions, each with three pigtails and one customer node.
Each pigsty from the source point to the first round has an edge, and its capacity is the initial number of pig in each pigsty.
Each customer has its own edge to the settlement point. capacity is the maximum number of customers that can buy.
In a certain round, all pigsty circles opened by the customer have one edge connected to the customer, and the capacity is + ∞.
Except for the last round, each round of pig I has a side connecting to the next round of pig I. The capacity is + ∞, indicating that the remaining pig in this round can be reserved for the next round.
Except for the last round, from all the pigsty circles opened in each round to the same pigsty circles in the next round, an edge must be connected to each other, indicating that they can flow freely.

 

 

 

# Include "stdio. H"
# Include "string. H"
# Include "queue"
# Define INF 9999999999
Int R [1001] [101];
Int visit [1001], vis [1001];
Int pre [1001], H [1001];
Using namespace STD;
Int n, m;
Int BFS (int s, int T)
{
Int P, I;
Memset (PRE, 0, sizeof (pre ));
Memset (visit, 0, sizeof (visit ));
Queue <int> q;
Pre [s] = s;
Visit [s] = 1;
Q. Push (s );
While (! Q. Empty ())
{
P = Q. Front ();
Q. Pop ();
For (I = 1; I <= n + 1; I ++)
{
If (! Visit [I] & R [p] [I])
{
Visit [I] = 1;
Pre [I] = P;
If (I = T)
Return 1;
Q. Push (I );
}
}
}
Return 0;
}
Int EK (int s, int T)
{
Int flow = 0, D, I;
While (BFS (S, T ))
{
D = inf;
For (I = T; I! = S; I = pre [I])
D = D> r [pre [I] [I]? R [pre [I] [I]: D;
For (I = T; I! = S; I = pre [I])
{
R [pre [I] [I]-= D;
R [I] [pre [I] + = D;
}
Flow + = D;
}
Return flow;
}
Int main ()
{
Int I, J, K, T, P;
While (scanf ("% d", & M, & N )! = EOF)
{
For (I = 1; I <= m; I ++)
Scanf ("% d", & H [I]);
Memset (R, 0, sizeof (R ));
Memset (VIS, 0, sizeof (VIS); // Apart from marking, VIS also remembers the person who opened the pigsty ..
For (I = 1; I <= N; I ++)
{
Scanf ("% d", & K );
For (j = 1; j <= K; j ++)
{
Scanf ("% d", & T );
If (vis [T] = 0) // if it is the first person, then the value of the source point is the number of pig heads ..
{
R [0] [I] + = H [T];
Vis [T] = I;
}
Else // otherwise, it is infinite.
{
R [vis [T] [I] = inf;
Pre [T] = I;
}
}
Scanf ("% d", & P );
R [I] [n + 1] = P;
}
Printf ("% d \ n", EK (0, n + 1 ));
}
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.