POJ 3281 Dining 最大流

饮料->牛->食物。

牛拆成两点。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
int ch;
while((ch=getchar())!=EOF)
{
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} struct Edge
{
int to,cap;
};
const int maxn = * ;
vector<int> g[maxn];
vector<Edge> edge;
int n,s,t;
int N, F, D;
int d[maxn];
int vis[maxn];
int cur[maxn]; void add(int u,int v,int cap)
{
//printf("%d -> %d : %d\n", u, v, cap);
edge.push_back((Edge){v, cap});
g[u].push_back(edge.size() - );
edge.push_back((Edge){u, });
g[v].push_back(edge.size() - );
}
void build()
{
for(int i = ; i <= n; i++)
g[i].clear();
edge.clear();
for(int i = ; i <= F; i++)
add(s,i,);
for(int i = F + ; i <= F + N; i++)
add(i, i + N, );
for(int i = + F + *N; i < + F + *N + D; i++)
add(i, t, );
for(int i = ; i <= N; i++)
{
int a, b;
scanf("%d%d", &a, &b);
for(int j = ; j <= a; j++)
{
int id;
scanf("%d", &id);
add(id, i + F, );
}
for(int j = ; j <= b; j++)
{
int id;
scanf("%d", &id);
add(i + N + F, id + N* + F, );
}
}
} bool bfs()
{
memset(vis, , sizeof(vis));
d[s] = ;
queue<int> q;
q.push(s);
vis[s] = true;
while(!q.empty())
{
int x = q.front(); q.pop();
for (int i = ; i < g[x].size(); i++)
{
Edge &e = edge[g[x][i]];
if(e.cap> && !vis[e.to])
{
vis[e.to] = true;
q.push(e.to);
d[e.to] = d[x] + ;
}
}
}
return vis[t];
} int dfs(int u,int f)
{
if(u==t || f==) return f;
for(int &i = cur[u]; i < g[u].size(); i++)
{
Edge &e = edge[g[u][i]];
if(e.cap> && d[e.to] == d[u] + )
{
int d = dfs(e.to, min(f,e.cap));
if(d>)
{
e.cap -= d;
edge[g[u][i]^].cap += d;
return d;
}
}
}
return ;
} int max_flow()
{
int res = ;
while(bfs())
{
memset(cur, , sizeof(cur));
int d;
while(d=dfs(s,INF))
{
res += d;
}
}
return res;
}
int main()
{
debug();
while(scanf("%d%d%d", &N, &F, &D) != EOF)
{
n = N* + F + D + ;
s = n-; t = n;
build();
printf("%d\n", max_flow());
}
return ;
}
上一篇:BestCoder Round #81 (div.2)C String


下一篇:【Android Studio安装部署系列】二十三、Android studio查看Gradle版本号