HDU 4292 最大流

点击打开链接

题意:有n个人,每个人都有自己喜欢的食物和饮料,如果一个人不能得到自己的喜欢的,则他将离开,问如何分配可以使剩下的人最多

思路:刚刚写的时候还以为是二分图匹配呢,无情wa,之前写过POJ的3281,一个类型嘛,没有区别,改了之后A之,这里解释一下为什么牛会和牛在连一条容量为1的边,这是因为如果一个牛可以匹配好多食物和饮料,如果不限制的话,这一个牛就可以匹配多种食物,但是我们要的是最多的牛的数量,加了这条边后每个牛就只能走一次了

#include <queue>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=10010;
struct edge{
    int to,cap,rev;
    edge(){}
    edge(int a,int b,int c){to=a;cap=b;rev=c;}
};
vector<edge> G[maxn];
int level[maxn],iter[maxn];
void add_edge(int from,int to,int cap){
    G[from].push_back(edge(to,cap,G[to].size()));
    G[to].push_back(edge(from,0,G[from].size()-1));
}
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int>que;
    level[s]=0;que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(unsigned int i=0;i<G[v].size();i++){
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f){
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t){
    int flow=0;
    while(1){
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,inf))>0) flow+=f;
    }
}
int main(){
    int n,f,d,a,b,c;
    char str[1010];
    while(scanf("%d%d%d",&n,&f,&d)!=EOF){
        for(int i=0;i<maxn;i++) G[i].clear();
        for(int i=1;i<=f;i++){
            scanf("%d",&a);
            add_edge(0,i,a);
        }
        for(int i=1;i<=d;i++){
            scanf("%d",&a);
            add_edge(f+n+i+n,n+n+f+d+1,a);
        }
        for(int i=1;i<=n;i++) add_edge(f+i,f+i+n,1);
        for(int i=1;i<=n;i++){
            scanf("%s",str);
            for(int j=0;j<f;j++){
                if(str[j]=='Y') add_edge(j+1,i+f,1);
            }
        }
        for(int i=1;i<=n;i++){
            scanf("%s",str);
            for(int j=0;j<d;j++){
                if(str[j]=='Y') add_edge(i+f+n,j+1+n+f+n,1);
            }
        }
        int ans=max_flow(0,n+n+f+d+1);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-07 12:20:51

HDU 4292 最大流的相关文章

hdu 4292 最大流 水题

很裸的一道最大流 格式懒得排了,注意把人拆成两份,一份连接食物,一份连接饮料 4 3 3 //4个人,3种食物,3种饮料 1 1 1 //食物每种分别为1 1 1 1 //饮料每种数目分别为1 YYN //第一个人对第1,2,3种食物的态度为接受,接受和拒绝 NYY YNY YNY YNY //第一个人对第1,2,3种饮料的态度为接受,拒绝和接受 YYN YYN NNY 3 1 #include<cstdio> 2 #include<iostream> 3 #include<

hdu 4292 网络最大流

http://acm.hdu.edu.cn/showproblem.php?pid=4292 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 colle

HDU 4292 Food(建图+最大流)

使用 PVRTC 压缩格式创建纹理(Creating textures in the PVRTC compression format) 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 有关该篇

hdu 4292 Food 最大流+拆点

Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2664    Accepted Submission(s): 899 Problem Description You, a part-time dining service worker in your college's dining hall, are now confus

hdu 4292 Food (最大流)

hdu 4292 Food 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 w

hdu 4888 最大流给出行列和求矩阵

第一步,考虑如何求是否有解.使用网络流求解,每一行和每一列分别对应一个点,加上源点和汇点一共有N+M+2个点.有三类边: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对应的点 -> 每一列对应的点,流量限制为 K 3. 每一列对应的点 -> 汇点,流量限制为该列的和 对上图做最大流,若源点出发的边和到达汇点的边全都满流,则有解,否则无解.若要求构造方案,则 (i,j) 对应的整数就是行 i–> 列 j 的流量. 第二步,考虑解是否唯一.显然,解唯一的充分必要条

hdu 4975最大流与4888类似但是有很吊的优化最大流

//来自潘神的优化 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0x3fffffff #define N 1100 struct node { int u,v,w,next; }bian[N*N*4]; int head[N],yong,dis[N],work[N]; void init(){ yong=0; memset(head,-1,si

Leapin&#39; Lizards (hdu 2732 最大流)

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1433    Accepted Submission(s): 586 Problem Description Your platoon of wandering lizards has entered a strange room in the labyr

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T