HDU 4292Food(网络流的最大流量)
职务地址:HDU 4292
水题。
因为每一个人仅仅能有1份,所以须要拆点限制流量。建图方法为,建一源点与汇点。将食物与源点相连,权值为食物额数量,将饮料与汇点相连,权值为饮料数量。。然后将人进行拆点为i和i‘,将相应的i与i’连边权值为1。将i与它所相应的YES的食物连边。将i‘与它所相应的YES的饮料连边。一次求最大流。
代码例如以下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int head[900], source, sink, nv, cnt;
int cur[900], num[900], d[900], pre[900], q[900];
struct node
{
int u, v, cap, next;
} edge[10000000];
void add(int u, int v, int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
void bfs()
{
memset(num,0,sizeof(num));
memset(d,-1,sizeof(d));
int f1=0, f2=0, i;
d[sink]=0;
num[0]=1;
q[f1++]=sink;
while(f1>=f2)
{
int u=q[f2++];
for(i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==-1)
{
d[v]=d[u]+1;
num[d[v]]++;
q[f1++]=v;
}
}
}
}
void isap()
{
memcpy(cur,head,sizeof(cur));
bfs();
int flow=0, u=pre[source]=source, i;
while(d[source]<nv)
{
if(u==sink)
{
int f=INF,pos;
for(i=source; i!=sink; i=edge[cur[i]].v)
{
if(f>edge[cur[i]].cap)
{
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=source; i!=sink; i=edge[cur[i]].v)
{
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
u=pos;
}
for(i=cur[u]; i!=-1; i=edge[i].next)
{
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
{
break;
}
}
if(i!=-1)
{
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else
{
if(--num[d[u]]==0) break;
int mind=nv;
for(i=head[u]; i!=-1; i=edge[i].next)
{
if(mind>d[edge[i].v]&&edge[i].cap)
{
mind=d[edge[i].v];
cur[u]=i;
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}
}
printf("%d\n",flow);
}
int main()
{
int n, f, d, i, j, x;
char s[300];
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
memset(head,-1,sizeof(head));
source=0;
cnt=0;
sink=2*n+f+d+1;
nv=sink+1;
for(i=1; i<=f; i++)
{
scanf("%d",&x);
add(source,i,x);
}
for(i=1; i<=d; i++)
{
scanf("%d",&x);
add(i+2*n+f,sink,x);
}
for(i=1; i<=n; i++)
{
add(i+f,i+f+n,1);
}
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=0; j<f; j++)
{
if(s[j]=='Y')
{
add(j+1,f+i,INF);
}
}
}
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=0; j<d; j++)
{
if(s[j]=='Y')
{
add(i+n+f,j+1+2*n+f,INF);
}
}
}
isap();
}
return 0;
}
HDU 4292Food(网络流的最大流量)的更多相关文章
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
- HDU 4888 (网络流)
Poroblem Redraw Beautiful Drawings (HDU4888) 题目大意 一个n行m列的矩形,只能填0~k的数字. 给定各行各列的数字和,判定有无合法的方案数.一解给出方案, ...
- hdu 4280 网络流
裸的网络流,递归的dinic会爆栈,在第一行加一句就行了 #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...
- Food HDU - 4292 网络流 拆点建图
http://acm.hdu.edu.cn/showproblem.php?pid=4292 给一些人想要的食物和饮料,和你拥有的数量,问最多多少人可以同时获得一份食物和一份饮料 写的时候一共用了2种 ...
- hdu 4240(最大流+最大流量的路)
Route Redundancy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1565&hdu 1569(网络流--最小点权值覆盖)
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU - 2828 网络流
题目大意 有n个灯,m个开关,由于线路乱接导致可能有多个开关对应一个灯(并联),有的灯在开关开的时候亮 有的灯在开关关的时候亮,[每个开关最多对应两盏灯],试找出一种开关的ON,OFF状态,使得所有灯 ...
- HDU 6634 网络流最小割模型 启发式合并
如果我们先手拿完所有苹果再去考虑花费的话. S -> 摄像头 -> 苹果 -> T 就相当于找到一个最小割使得S和T分开. ans = sum - flow. 然后对于这一个模型, ...
随机推荐
- Mvc后台接收 参数
@Html.TextAreaFor(m => m.Emps, new { @class = "easyui-validatebox", @style = "heig ...
- 移植kl档,但调用默认Generic.kl解决的方法
从飞机到现在移植模型来开发遥控器按键,调查发现,大部分的功能键始终没有任何效果. 跑 dumpsys input 要查看当前的关键kl, 查找KeyLayoutFile它原来是默认Generic.kl ...
- Android AlarmManager报警的实现
什么是AlarmManager? AlarmManager它是Android经常使用的系统-Level提醒服务,我们指定为广播中的特定时间Intent. 我们设定一个时间,然后在该时间到来时.Alar ...
- javascript焦点图(能够自己主动切换 )
/* 思路总结: 1.实现图片滚动的function.鼠标经时候获取当前li的index.设置ndex自己主动递增的函数.实现淡入淡出效果的函数 2.整个实现效果一传递index为主线 3.我的编写代 ...
- sql二进制数据权限
(3为权限组合值,结果为1=列表 2=新建 4=修改 8=删除) select 3 & 1 select 3 & 2 select 3 & 4 select 3 & 2 ...
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...
- 答读者问(8):相关Java问题涉及到学习
近来的,我收到一个研究生朋友的电子邮件,一般内容如下面: 日 (本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,我们的聊天号码:245924426.欢 ...
- [LeetCode145]Binary Tree Postorder Traversal
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- pragma message任务
pragma message它是用来告诉程序猿,在编译的程序信息.和outputdebugstr则是告诉程序猿.程序在执行时期的信息. 以下就以一个样例来解说pragma message. 配合#if ...
- Google API快速生成QR二维码
Google API快速生成QR二维码 现在来说生成二维码最简单的方法是使用Google Chart API来实现,再次膜拜Google大神- Google Chart API是一套可以让你在线生成报 ...