设为首页 收藏本站
查看: 1050|回复: 0

[经验分享] HDU 4292 Food (SAP | Dinic )

[复制链接]

尚未签到

发表于 2015-9-19 14:30:10 | 显示全部楼层 |阅读模式
Food
  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1491    Accepted Submission(s): 534




Problem Description

  You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible.
  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.

  


Input

  There are several test cases.
  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).

  


Output

  For each test case, please print a single line with one integer, the maximum number of people to be satisfied.

  


Sample Input



4 3 3

1 1 1

1 1 1

YYN

NYY

YNY

YNY

YNY

YYN

YYN

NNY

  


Sample Output



3

  


Source

2012 ACM/ICPC Asia Regional Chengdu Online

  


Recommend

liuyiding  
  
  题意:有F种食物 D种饮料 它们都有一定的数量 有N个人 每个人都有自己喜欢吃的食物和饮料 (每个人至少要一种食物和饮料) 只有能满足他的要求时他才会接服务 求最大能满足多少人?
思路:网络流 建一超级源点 汇点 源点与食物相连 边权为其数量,汇点与饮料相连 边权也为其数量 把人分成两个点 之间的边权为1 每个人与之需要的食物和饮料相连 边权为1 (或者INF)
   当然,这题和POJ 3281 Dining很类似:http://poj.org/problem?id=3281
  


DSC0000.gif DSC0001.gif


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int VM=1010;
const int EM=200010;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int N,F,D,cnt,head[VM],map[110][110];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM];
void addedge(int cu,int cv,int cw){
edge[cnt].to=cv;  edge[cnt].cap=cw;  edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu;  edge[cnt].cap=0;   edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int src,des;
int SAP(int n){
int max_flow=0,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-1;
memset(dep,0,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=n;
for(int i=0;i<=n;i++)
cur=head; // 初始化当前弧为第一条弧
while(dep[src]<n){
int flag=0;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-1;v=pre[v]){     // 路径回溯更新残留网络
id=cur[v];
edge[id].cap-=aug[des];
edge[id^1].cap+=aug[des];
aug[v]-=aug[des];   // 修改可增广量,以后会用到
if(edge[id].cap==0) // 不回退到源点,仅回退到容量为0的弧的弧尾
u=v;
}
}
for(int i=cur;i!=-1;i=edge.nxt){
v=edge.to;    // 从当前弧开始查找允许弧
if(edge.cap>0 && dep==dep[v]+1){  // 找到允许弧
flag=1;
pre[v]=u;
cur=i;
aug[v]=min(aug,edge.cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep]==0)    /* gap优化,层次树出现断层则结束算法 */
break;
mindep=n;
cur=head;
for(int i=head;i!=-1;i=edge.nxt){
v=edge.to;
if(edge.cap>0 && dep[v]<mindep){
mindep=dep[v];
cur=i;   // 修改标号的同时修改当前弧
                }
}
dep=mindep+1;
gap[dep]++;
if(u!=src)  // 回溯继续寻找允许弧
u=pre;
}
}
return max_flow;
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d%d",&N,&F,&D)){
cnt=0;
memset(head,-1,sizeof(head));
int f,d;
src=0,  des=F+2*N+D+1;
for(int i=1;i<=N;i++){
scanf("%d%d",&f,&d);
int x;
for(int j=1;j<=f;j++){  
scanf("%d",&x);
addedge(x,F+i,1);   //食物和牛1(将牛分成两点)相连
            }
for(int j=1;j<=d;j++){
scanf("%d",&x);
addedge(F+N+i,F+2*N+x,1);   //牛2和饮料相连
            }
addedge(F+i,F+N+i,1);   //牛1和牛2相连,保证没头牛只吃一种食物和饮料
        }
for(int i=1;i<=F;i++)
addedge(src,i,1);   //超级源点与食物相连
for(int i=1;i<=D;i++)
addedge(F+2*N+i,des,1);     //饮料与超级汇点相连
printf("%d\n",SAP(des+1));
}
return 0;
}
View Code   
  本题代码:
  



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int VM=1010;
const int EM=200010;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int N,F,D,cnt,head[VM],map[110][110];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM];
void addedge(int cu,int cv,int cw){
edge[cnt].to=cv;  edge[cnt].cap=cw;  edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu;  edge[cnt].cap=0;   edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int src,des;
int SAP(int n){
int max_flow=0,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-1;
memset(dep,0,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=n;
for(int i=0;i<=n;i++)
cur=head; // 初始化当前弧为第一条弧
while(dep[src]<n){
int flag=0;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-1;v=pre[v]){     // 路径回溯更新残留网络
id=cur[v];
edge[id].cap-=aug[des];
edge[id^1].cap+=aug[des];
aug[v]-=aug[des];   // 修改可增广量,以后会用到
if(edge[id].cap==0) // 不回退到源点,仅回退到容量为0的弧的弧尾
u=v;
}
}
for(int i=cur;i!=-1;i=edge.nxt){
v=edge.to;    // 从当前弧开始查找允许弧
if(edge.cap>0 && dep==dep[v]+1){  // 找到允许弧
flag=1;
pre[v]=u;
cur=i;
aug[v]=min(aug,edge.cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep]==0)    /* gap优化,层次树出现断层则结束算法 */
break;
mindep=n;
cur=head;
for(int i=head;i!=-1;i=edge.nxt){
v=edge.to;
if(edge.cap>0 && dep[v]<mindep){
mindep=dep[v];
cur=i;   // 修改标号的同时修改当前弧
                }
}
dep=mindep+1;
gap[dep]++;
if(u!=src)  // 回溯继续寻找允许弧
u=pre;
}
}
return max_flow;
}
int main(){
//freopen("input.txt","r",stdin);
char str[220];
while(~scanf("%d%d%d",&N,&F,&D)){
cnt=0;
memset(head,-1,sizeof(head));
int f,d;
src=0, des=F+2*N+D+1;
for(int i=1;i<=F;i++){
scanf("%d",&f);
addedge(src,i,f);
}
for(int i=F+2*N+1;i<=F+2*N+D;i++){
scanf("%d",&d);
addedge(i,des,d);
}
for(int i=1;i<=N;i++){
scanf("%s",str);
for(int j=0;j<F;j++)
if(str[j]=='Y')
addedge(j+1,F+i,1);     //这里权值为INF亦可
        }
for(int i=1;i<=N;i++){
scanf("%s",str);
for(int j=0;j<D;j++)
if(str[j]=='Y')
addedge(F+N+i,F+2*N+j+1,1);     //这里权值为INF亦可
        }
for(int i=F+1;i<=F+N;i++)   
addedge(i,i+N,1);   //将人拆分成两点,边权为1,为了控制最多的人得到一个食物以及一瓶饮料
printf("%d\n",SAP(des+1));
}
return 0;
}
  
  



#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=1010;
const int EM=200010;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int N,F,D,cnt,head[VM],src,des;
int dep[VM];
void addedge(int cu,int cv,int cw){
edge[cnt].to=cv;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu;    edge[cnt].cap=0;    edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int BFS(){
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-1,sizeof(dep));
dep[src]=0;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head;i!=-1;i=edge.nxt){
int v=edge.to;
if(edge.cap>0 && dep[v]==-1){
dep[v]=dep+1;
q.push(v);
}
}
}
return dep[des]!=-1;
}
int DFS(int u,int minx){
if(u==des)
return minx;
int tmp;
for(int i=head;i!=-1;i=edge.nxt){
int v=edge.to;
if(edge.cap>0 && dep[v]==dep+1 && (tmp=DFS(v,min(minx,edge.cap)))){
edge.cap-=tmp;
edge[i^1].cap+=tmp;
return tmp;
}
}
dep=-1;
return 0;
}
int Dinic(){
int ans=0,tmp;
while(BFS()){
while(1){
tmp=DFS(src,INF);
if(tmp==0)
break;
ans+=tmp;
}
}
return ans;
}
int main(){
//freopen("input.txt","r",stdin);
char str[220];
while(~scanf("%d%d%d",&N,&F,&D)){
cnt=0;
memset(head,-1,sizeof(head));
int f,d;
src=0, des=F+2*N+D+1;
for(int i=1;i<=F;i++){
scanf("%d",&f);
addedge(src,i,f);
}
for(int i=F+2*N+1;i<=F+2*N+D;i++){
scanf("%d",&d);
addedge(i,des,d);
}
for(int i=1;i<=N;i++){
scanf("%s",str);
for(int j=0;j<F;j++)
if(str[j]=='Y')
addedge(j+1,F+i,1);
}
for(int i=1;i<=N;i++){
scanf("%s",str);
for(int j=0;j<D;j++)
if(str[j]=='Y')
addedge(F+N+i,F+2*N+j+1,1);
}
for(int i=F+1;i<=F+N;i++)
addedge(i,i+N,1);
printf("%d\n",Dinic());
}
return 0;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-115918-1-1.html 上篇帖子: sap学习笔记一 下篇帖子: SAP change_document
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表