hdu4292
来源:互联网 发布:dd是什么意思网络用语 编辑:程序博客网 时间:2024/06/05 18:05
Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3851 Accepted Submission(s): 1289
The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.
You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink.
Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service.
For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.
The second line contains F integers, the ith number of which denotes amount of representative food.
The third line contains D integers, the ith number of which denotes amount of representative drink.
Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.
Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.
Please process until EOF (End Of File).
4 3 31 1 11 1 1YYNNYYYNYYNYYNYYYNYYNNNY
3
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1000
#define MAXM 200000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN], cur[MAXN];
bool vis[MAXN];
int N, F, D;
int source, sink;//超级源点 超级汇点
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
Edge E1 = {u, v, w, 0, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
void getMap()
{
int a;
source = 0, sink = 2*N + F + D + 1;
//人拆点后 编号从1到2*N
for(int i = 1; i <= N; i++)//对人拆点
addEdge(i, i+N, 1);//只能选一种食物和一种饮料
//食物编号从2*N+1到2*N+F
for(int i = 1; i <= F; i++)
{
scanf("%d", &a);
addEdge(source, 2*N+i, a);//源点 到 食物建边
}
//饮料编号2*N+F+1到2*N+F+D
for(int i = 1; i <= D; i++)
{
scanf("%d", &a);
addEdge(2*N+F+i, sink, a);//饮料 到 汇点建边
}
char str[300];
for(int i = 1; i <= N; i++)
{
scanf("%s", str);
for(int j = 0; j < F; j++)
{
if(str[j] == 'Y')//第i个人 接受 第j+1种食物
addEdge(2*N+j+1, i, 1);
}
}
for(int i = 1; i <= N; i++)
{
scanf("%s", str);
for(int j = 0; j < D; j++)
{
if(str[j] == 'Y')//第i个人 接受 第j+1种饮料
addEdge(i+N, 2*N+F+j+1, 1);
}
}
}
/*bool BFS(int s, int t)
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, false, sizeof(vis));
dist[s] = 0;
vis[s] = true;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow)
{
dist[E.to] = dist[u] + 1;
if(E.to == t) return true;
vis[E.to] = true;
Q.push(E.to);
}
}
}
return false;
}*/
bool BFS(int s,int t)
{
queue <int>Q;//存东西用
memset(dist,-1,sizeof(dist));
memset(vis,false,sizeof(vis));
dist[s]=0;//分层,先给第一层个0:
vis[s]=true;
Q.push(s);
while(!Q.empty())
{ int u=Q.front();//从门口拿走一个数;
Q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{ Edge a=edge[i];
if(!vis[a.to]&&a.cap>a.flow)
{ dist[a.to]=dist[u]+1;
if(a.to==t)return true;
vis[a.to]=true;
Q.push(a.to);
}
}
}
return false;
}
int DFS(int x, int a, int t)
{
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next)
{
Edge &E = edge[i];
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), t)) > 0)
{
edge[i].flow += f;//为啥加的 都是反的
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
//这样写有一个好处,就是如果想得到最大流的流向,
//那么直接就把flow提出来就可以了
int Maxflow(int s, int t)
{
int flow = 0;
while(BFS(s, t))
{
memcpy(cur, head, sizeof(head));
flow += DFS(s, INF, t);
}
return flow;
}
int main()
{
while(scanf("%d%d%d", &N, &F, &D) != EOF)
{
init();
getMap();
printf("%d\n", Maxflow(source, sink));
}
return 0;
}
- hdu4292
- hdu4292
- hdu4292
- HDU4292--Food
- HDU4292--Food
- HDU4292 水题..
- hdu4292 food
- HDU4292 网络流
- hdu4292 Food (ISAP算法)
- hdu4292 Food --- 最大流
- (最大流)HDU4292 Food
- hdu4292(最大流)
- hdu4292网络流建图
- 解题报告 之 HDU4292 Food
- HDU4292 Food(基础最大流)
- 2012成都赛区网络赛 hdu4292
- HDU4292--Food拆点网络流
- hdu4292 Food 网络最大流 拆点
- linux动态链接库导出函数控制
- python中的面向对象
- CodeForces
- 需求分析的图形工具(层次方框 warnier IPO)
- Butterknife 8.4.0版本的使用:
- hdu4292
- Linux 如何打开端口
- C++Primer第五版 第十三章习题答案(1~10)
- ext4_mpage_readpages
- poj3481(通过stl中的set,以及pair和make_pair简单通过)
- POJ1064
- Resource.arsc文件格式解析
- Css练习代码暂存
- Inpho.DtMaster.v1.0.0 1CD