POJ3281 Dining【Restrictions on network flow/points/dismantling points】

Topic link: POJ3281 Dining

Question meaning: f food and d drinks have their own numbers, n cows will be happy when they eat the food and drink they want, and ask how many cows to be happy at most;

Analysis: For dairy cows, there is an upper limit of 1, and for dairy cows to dismantle points and build edges, the flow rate is 1 (note the number of points);

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<string.h>
#include<cstring>
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn=1200;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
struct node {int to,cap,rev;};
int dep[maxn],n,m,S,T;
vector<node>v[maxn];
void add(int from,int to,int cap)  //重边情况不影响
{
    v[from].pb({to,cap,(int)v[to].size()});
    v[to].pb({from,0,(int)v[from].size()-1});
}
int bfs(int s,int t)
{
    queue<int>q;
    memset(dep,-1,sizeof(dep));
    dep[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<v[u].size();i++)
            if(v[u][i].cap>0&&dep[v[u][i].to]==-1)
            {
                dep[v[u][i].to]=dep[u]+1;
                q.push(v[u][i].to);
            }
    }
    return dep[t]!=-1;
}
int dfs(int u,int t,int micap)
{
    if(u==t)return micap;
    int tmp;
    for(int i=0;i<v[u].size();i++)
    {
        node &now=v[u][i];
        if(now.cap>0 && dep[u]+1==dep[now.to] && (tmp=dfs(now.to,t,min(now.cap,micap))))
        {
            now.cap-=tmp;
            v[now.to][now.rev].cap+=tmp;
            return tmp;
        }
    }
    return 0;
}
int dinic(int s,int t)
{
    int ans=0,tmp;
    while(bfs(s,t))
        while(1)
        {
            tmp=dfs(s,t,inf);
            if(tmp==0)break;
            ans+=tmp;
        }
    return ans;
}
int main()
{
    int n,f,d;scanf("%d%d%d",&n,&f,&d);
    int s=0,t=2*n+f+d+1;memset(v,0,sizeof(v));
    for(int i=1;i<=f;i++) add(s,i,1);
    for(int i=1;i<=d;i++) add(i+f,t,1);
    int res=f+d;
    for(int i=1;i<=n;i++)
    {
        int x,y,kk;scanf("%d%d",&x,&y);add(i+res,i+res+n,1);
        while(x--) {scanf("%d",&kk);add(kk,i+res,1);}
        while(y--) {scanf("%d",&kk);add(i+res+n,kk+f,1);}
    }
    printf("%d\n",dinic(s,t));
}

 

Guess you like

Origin blog.csdn.net/qq_43813163/article/details/105159899