poj-1149 (maximum flow)

The meaning of the question: There is a pig farm, and the manager has no keys. This pig farm has M pig sties and N customers. Each customer has some keys to the pig sties. Each customer needs some pigs. Ask your factory manager the most. How many pigs can you sell? There is a condition here that the factory manager can adjust the number of pigs in the pigsty without locking the door after a customer buys it, for example, transfer all the pigs in several pigpens to one pigpen (this condition will affect the Later construction), then close the door and wait for the next customer;

Problem-solving idea: Because the number of pigs in the sty is limited by the quantity that customers can buy, the customer is regarded as a node to build a map:

First, set up a source point, which is connected to the first customer of each pigpen, representing the incoming traffic

Then the capacity of every two customers that can be directly connected is set to be infinite, because after the previous customer has finished buying, I can try to concentrate the pigs from several nearby sties into the sty where the next customer can open the door (here directly Connected means that the key is repeated), so the next customer flows into as much traffic as possible, so the capacity is set to infinite;

Finally, each customer node is connected to a sink, and the capacity is the number of pigs each customer needs;

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 200005
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
    int next;
    int to;
    int w;
}edge[maxn];
int cnt;
int head[maxn];
int cur[maxn];
int depth[maxn];
int belong[maxn];
int a[maxn];
int s,e,n,m;
void add(int u,int v,int w)
{
    edge[cnt].next=head[u];edge[cnt].w=w;
    edge[cnt].to=v;head[u]=cnt++;
    edge[cnt].next=head[v];edge[cnt].w=0;
    edge[cnt].to=u;head[v]=cnt++;
}
bool bfs()
{
    memset(depth,0,sizeof(depth));
    depth[s]=1;
    queue <int> que; que.push (s);
    while(!que.empty())
    {
        int u = que.front (); que.pop ();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(!depth[v]&&edge[i].w>0)
            {
                depth[v]=depth[u]+1;que.push(v);
            }
        }
    }
    return depth[e];
}
int dfs (int u, int maxflow)
{
    int tempflow;
    if(u==e)
        return maxflow;
    int add=0;
    for(int &i=cur[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(depth[v]==depth[u]+1&&edge[i].w>0&&(tempflow=dfs(v,min(maxflow-add,edge[i].w))))
        {
            edge[i].w-=tempflow;
            edge[i^1].w+=tempflow;
            add+=tempflow;
            if(maxflow==add)
                break;
        }
    }
    return add;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        for(int i=0;i<=2*n;i++)
            cur[i]=head[i];
        while(int temp=dfs(s,inf))
            years+=temp;
    }
    return ans;
}
intmain()
{
    int x,y,w;
    int k;
    int d;
    scanf("%d%d",&m,&n);
    s=0;e=n+1;
    memset(head,-1,sizeof(head));
    cnt=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&k);
        y=0;
        for(int j=1;j<=k;j++)
        {
            scanf("%d",&x);
            if(belong[x]==0)
            {
                y+=a[x];
            }
            else
            {
                add(belong[x],i,inf);
            }
            belong[x]=i;
        }
        add(s,i,y);
        scanf("%d",&d);
        add(i,e,d);
    }
    printf("%d\n",dinic());
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292535&siteId=291194637