POJ 3281 Dining 【网络流建模汇总】最大流

很经典的一道网络流了

相信网上的题解也很多很多


这里的重点放在两张图上


是上图还是下图



就会明白这个题到底什么意思的

点权和边权到底如何解释


//#include<bits/stdc++.h>
#include<stdio.h>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string.h>
using namespace std;

const int maxn=11000;
const int maxm=120000;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt,cap,flow;
}edge[maxm];
int tol,Head[maxn];

void init(){
    memset(Head,-1,sizeof(Head));
    tol=2;
}

void addedge(int u,int v,int w,int rw=0){
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].flow=0;
    edge[tol].nxt=Head[u];
    Head[u]=tol++;
    edge[tol].to=u;
    edge[tol].cap=rw;
    edge[tol].flow=0;
    edge[tol].nxt=Head[v];
    Head[v]=tol++;
}

int Q[maxn],dep[maxn],cur[maxn],sta[maxn];

bool bfs(int s,int t,int n){
    int Front=0,Tail=0;
    memset(dep,-1,sizeof(dep[0])*(n+1));
    dep[s]=0;
    Q[Tail++]=s;
    while(Front<Tail){
        int u=Q[Front++];
        for(int i=Head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if (edge[i].cap>edge[i].flow&&dep[v]==-1){
                dep[v]=dep[u]+1;
                if (v==t) return true;
                Q[Tail++]=v;
            }
        }
    }
    return false;
}

int dinic(int s,int t,int n){
	int maxflow=0;
	while(bfs(s,t,n)){
		for(int i=0;i<n;i++) cur[i]=Head[i];
		int u=s,tail=0;
		while(cur[s]!=-1){
			if (u==t) {
				int tp=INF;
				for(int i=tail-1;i>=0;i--)
					tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
				maxflow+=tp;
				for(int i=tail-1;i>=0;i--){
					edge[sta[i]].flow+=tp;
					edge[sta[i]^1].flow-=tp;
					if (edge[sta[i]].cap-edge[sta[i]].flow==0) tail=i;
				}
				u=edge[sta[tail]^1].to;
			}
			else if (cur[u]!=-1&&edge[cur[u]].cap>edge[cur[u]].flow&&dep[u]+1==dep[edge[cur[u]].to]){
				sta[tail++]=cur[u];
				u=edge[cur[u]].to;
			}
			else{
				while(u!=s&&cur[u]==-1) u=edge[sta[--tail]^1].to;
				cur[u]=edge[cur[u]].nxt;
			}
		}
	}
	return maxflow;
}

int n,f,d;
int s,t,tot;

int main(){
    //freopen("input.txt","r",stdin);
    while(scanf("%d%d%d",&n,&f,&d)!=EOF){
        init();
        s=0;t=f+n+n+d+1;tot=t+1;
        int x,y,num;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            while(x--){
                scanf("%d",&num);
                addedge(num,f+i,1);
            }
            while(y--){
                scanf("%d",&num);
                addedge(f+i+n,f+n+n+num,1);
            }
            addedge(f+i,f+n+i,1);
        }
        for(int i=1;i<=f;i++) addedge(s,i,1);
        for(int i=1;i<=d;i++) addedge(i+f+n+n,t,1);
        printf("%d\n",dinic(s,t,tot));
    }
    return 0;
}

细节:

点的编号容易写错,注意在addedge的时候,带几个数值确定检验一下

全部评论

相关推荐

10-11 14:44
济南大学 Java
点赞 评论 收藏
分享
牛客40297450...:不是研究生强,是你强
点赞 评论 收藏
分享
09-16 14:43
已编辑
华南农业大学 游戏后端
背景&nbsp;双一流本硕&nbsp;双非大圆满&nbsp;只找游戏开发相关的岗位。&nbsp;8&nbsp;月初开始秋招到现在&nbsp;投了四五十家吧,&nbsp;目前两&nbsp;offer,&nbsp;不打算继续投了,把剩下的流程走完就开始沉淀了。目前两&nbsp;offer&nbsp;一个是网易互娱测开&nbsp;base&nbsp;广州,一个是江娱互动客户端开发&nbsp;base&nbsp;北京。应该确定网易这个了,说实话北京这个我挺想去的,这家的产品和工作氛围我了解了也不错,是那种踏实做事的,可惜我是广东人。网易的测开是调剂的二志愿,看了下有内部转岗机会,所以打算后面找个时间提前实习,沉淀下再做一个&nbsp;demo&nbsp;作品,写一些&nbsp;shader,增强下图形学渲染的能力,再学点编辑器开发。看到时候内部转岗或者春招继续投客户端开发这样。后面还能再动摇的话应该就灵犀或者腾子了吧(假如这两家确认的是客户端开发岗的话)。-----------------------补下timeline网易互娱&nbsp;测开&nbsp;8.2笔试&nbsp;&nbsp;8.21&nbsp;技术面&nbsp;&nbsp;8.29&nbsp;leader&amp;HRBP面(终面)&nbsp;9.8&nbsp;录用审核(之前一直显示面试中)9.14&nbsp;oc江娱互动&nbsp;客户端开发&nbsp;8.29主程面&nbsp;9.3&nbsp;制作人面&nbsp;9.5&nbsp;BOSS面&nbsp;9.11&nbsp;口头OC&nbsp;9.15&nbsp;正式offer后面考虑了一下&nbsp;&nbsp;感觉还是能走开发就开发吧,测开不太感兴趣,要内部活水转岗还要满1年才能申请。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务