HDU 4292 Food (split point, maximum Stream)

Source: Internet
Author: User

[Cpp]
/*
Maximum Flow Problems
Split points: Increase the super source points and sink points. The source points are connected to the food, and the weight of the edge is the largest number of foods. The drink is connected to the sink point, and the weight is the largest number of drinks;
Split people into two, one connected to food, and the other connected to drinks. The weight can be 1 or INF without any impact.
Connect the two split nodes. The Edge Weight is 1.
*/
 
# Include <cstdio>
# Include <cstring>
Const int nmax= 2007;
Const int INF = 0x3fffffff;
Int N, F, D;
Int NN;
Struct Adj
{
Int v, w;
Int next;
Adj (){}
Adj (int v, int w, int next): v (v), w (w), next (next ){}
} Adj [100 * nMax];
Int head [nMax];
Int cnt;
Int dis [nMax], num [nMax];
 
Void addEdge (int u, int v, int w)
{
Adj [cnt] = Adj (v, w, head [u]);
Head [u] = cnt ++;
Adj [cnt] = Adj (u, 0, head [v]);
Head [v] = cnt ++;
}
 
Int min (int a, int B)
{
Return a <B? A: B;
}
 
Int dfs (int u, int s, int d, int cost)
{
If (u = d) return cost;
Int I;
Int ans = 0;
Int _ min = NN;
For (I = head [u]; I! =-1; I = adj [I]. next)
{
Int v = adj [I]. v;
If (adj [I]. w)
{
If (dis [v] + 1 = dis [u])
{
Int t = dfs (v, s, d, min (adj [I]. w, cost ));
Adj [I]. w-= t;
Adj [I ^ 1]. w + = t;
Ans + = t;
Cost-= t;
If (dis [s] = NN) return ans;
If (! Cost) break;
}
If (_ min> dis [v])
_ Min = dis [v];
}
}
If (! Ans)
{
If (-- num [dis [u] = 0) dis [s] = NN;
Dis [u] = _ min + 1;
++ Num [dis [u];
}
Return ans;
}
 
Int isap (int s, int d)
{
Memset (dis, 0, sizeof (dis ));
Memset (num, 0, sizeof (num ));
Num [0] = NN;
Int ans = 0;
While (dis [s] <NN)
Ans + = dfs (s, s, d, INF );
Return ans;
}
 
Int main ()
{
// Freopen ("e: // data. in", "r", stdin );
While (scanf ("% d", & N, & F, & D )! = EOF)
{
Int I, j;
Int;
Char s [nMax];
Memset (head,-1, sizeof (head ));
Cnt = 0;
For (I = 1; I <= F; ++ I)
{
Scanf ("% d", & );
AddEdge (0, I, );
}
For (I = 1; I <= D; ++ I)
{
Scanf ("% d", & );
AddEdge (F + 2 * N + I, F + 2 * N + D + 1, );
}
For (I = 1; I <= N; ++ I)
{
AddEdge (F + I, F + N + I, 1 );
}
For (I = 1; I <= N; ++ I)
{
Scanf ("% s", s + 1 );
For (j = 1; j <= F; ++ j)
If (s [j] = 'y ')
AddEdge (j, F + I, INF );
}
For (I = 1; I <= N; ++ I)
{
Scanf ("% s", s + 1 );
For (j = 1; j <= D; ++ j)
If (s [j] = 'y ')
AddEdge (F + N + I, F + 2 * N + j, INF );
}
NN = F + 2 * N + D + 2;
Int ans = isap (0, NN-1 );
Printf ("% d \ n", ans );
}
Return 0;
}

Related Article

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.