Poj3281 network stream (split point and add source)

Source: Internet
Author: User

329 ms. Create a Source Vertex sink by yourself, connect the Source Vertex to all the food, and split a cow into two points. Ox I-1, Ox I-2, linking food with ox I-1, then all ox I, connecting ox I-1 and ox I-2, linking ox I-2 to drinks. Finally, all the drinks are connected to the sink.

The figure is like this: source point s --> food F --> OX 1 --> OX 2 --> drink D --> sink point t

Split the ox into two dots and place them in the middle to connect the food and drinks at the same time. Then the two nodes of the ox are connected to each other, with a capacity of 1. The Food and Beverage data is read from OX 1 and OX 2.


# Include <iostream>
# Include <cstdio> // EK () algorithm. Time complexity (VE ^ 2)
# Include <queue>
# Include <cstring>

Using namespace std;
Const int maxn = 500;
Const int INF = 0x3fffffff;

Int N, F, D;
Int Fi [maxn];
Int Di [maxn];

Int g [maxn] [maxn];
Int flow [maxn], pre [maxn];
Bool vis [maxn];
Int n;

Int bfs (int s, int e ){
Memset (pre,-1, sizeof (pre ));
Memset (vis, false, sizeof (vis ));
Queue <int> q;
Vis [s] = true;
For (int I = 1; I <= n; I ++)
Flow [I] = INF;
Q. push (s );
While (! Q. empty ()){
Int now = q. front ();
Q. pop ();
If (now = n) break;
For (int I = 1; I <= n; I ++) {// search for the shortest traffic
If (! Vis [I] & g [now] [I]> 0 ){
Vis [I] = true;
Flow [I] = min (flow [now], g [now] [I]);
Pre [I] = now;
Q. push (I );
}
}
}
If (! Vis [e] | e = 1) // The complete augmenting or Source Vertex coincidence cannot be found.
Return-1;
Else
Return flow [e];
}

Int EK (int s, int e ){
Int temp, d, res, maxflow;
Maxflow = 0;
While (d = bfs (s, e ))! =-1 ){
Maxflow + = d;
Temp = n;
While (temp! = 1 ){
Res = pre [temp];
G [res] [temp]-= d; // forward edge
G [temp] [res] + = d; // reverse edge
Temp = res;
}
}
Return maxflow;
}

Int main (){
Memset (g, 0, sizeof (g ));
Memset (flow, 0, sizeof (flow ));
Cin> N> F> D;
N = 1 + F + N + D + 1;
For (int I = 1; I <= F; I ++)
{
G [1] [I + 1] = 1;
}
For (int I = 1; I <= D; I ++)
{
G [1 + F + N + I] [n] = 1;
}
For (int I = 1; I <= N; I ++)
{
G [1 + F + I] [1 + F + N + I] = 1;
}
Int nf, nd;
For (int I = 1; I <= N; I ++)
{
Cin> nf> nd;
Int x;
While (nf --)
{
Cin> x;
G [1 + x] [1 + F + I] = 1;
}
While (nd --)
{
Cin> x;
G [1 + F + N + I] [1 + F + N + x] = 1;
}
}
Cout <EK (1, n) <endl;
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.