HDU 4292 Food 图论,网络流,建模
题目链接
http://acm.split.hdu.edu.cn/showproblem.php?pid=4292
或者
http://acm.hdu.edu.cn/showproblem.php?pid=4292
// author:latstars
// time:2016-09-02 19:23:27
// 题目:HDU 4292
// 分类:图论,网络流,建模
// 题意:给定食物种数F,人数N,饮料种数D,每种食物的数量,每种饮料的数量,以及每个人能够接受的食物和饮料
// 每个人只使用一种食物和饮料,问这些食物最多能够满足多少人的食用要求,每个人背满足表示他同时得到自己能接受的食物和饮料
// 思路:从源点向每种食物连一条容量为食物数量的边
// 每种饮料向汇点连一条容量为饮料数量的边
// 然后在对每个人与他能够接受的食物连一条边,容量为1,
// 每个人这里限制最大能够流过1的流量
// 然后人再向他能接受的饮料连一条容量为1的边
#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1007;
struct Edge{
int from, to, cap, flow;
Edge(int a, int b, int c, int d)
{
from = a;
to = b;
cap = c;
flow = d;
}
};
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void AddEdge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()){
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t||a==0) return a;
int flow=0,f;
for(int & i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
this->s=s;this->t=t;
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
};
int man_drink[206][206]; //源是0,食物是1~200,人是201~400,人2是401~600,饮料是601~800,汇点是999
int food_man[206][206];
int food[206];
int drink[206];
Dinic fuck;
int main()
{
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
int n, f, d;
while (cin >> n >> f >> d)
{
for (int i = 1; i <= f; i++)
cin >> food[i];
for (int i = 1; i <= d; i++)
cin >> drink[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= f; j++)
{
char k;
cin >> k;
// cout<<k;
if (k == 'Y')food_man[j][i] = 1;
else food_man[j][i] = 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= d; j++)
{
char k;
cin >> k;
if (k == 'Y')man_drink[i][j] = 1;
else man_drink[i][j] = 0;
}
fuck.edges.clear();
for(int i=0;i<maxn;i++) fuck.G[i].clear();
memset(fuck.d, 0, sizeof(fuck.d));
memset(fuck.cur, 0, sizeof(fuck.cur));
for (int i = 1; i <= f; i++)
fuck.AddEdge(0, i, food[i]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= f; j++) //原点0,食物i,人200+i,人2 400+i,饮料600+i,汇点999
if (food_man[j][i]) fuck.AddEdge(j, f + i, 1);
for (int i = 1; i <= n; i++)
fuck.AddEdge(f + i, f+n + i, 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= d; j++)
if (man_drink[i][j]) fuck.AddEdge(f+n + i, f+n*2 + j, 1);
for (int i = 1; i <= d; i++)
fuck.AddEdge(f+n*2 + i, f+2*n+d+1, drink[i]);
cout << fuck.Maxflow(0, f+2*n+d+1) << endl;
}
//system("pause");
return 0;
}