POJ 1149 PIGS 【网络流】

题意:

m n   //有m个猪圈,n个人卖猪。

a1...am    //编号为i的猪圈里有ai头猪。

b1 c1...cb1 d1   //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都是锁着的   他最多买di头猪

.

.

.

bn c1...cbn dn

注意的是假如某买主有第一和第二个猪圈的钥匙,那么他买完猪以后这两个猪圈的个数我们可以随意调整,调整完将猪圈锁上。

求:最多能卖多少猪

思路:

源点到每个猪圈建边,容量为这个猪圈的猪的数量,然后买主与他有钥匙的猪圈建边,容量inf。买主与其它与以前与他在同一个猪圈买过猪的买主建边,容量是inf,然后每个买主与汇点建边,容量是他最多买的猪的数目。跑一下最大流。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string.h>
#include<vector>
#define MAXN 1500
#define MAXM 50000
using namespace std;
const int inf=0x3f3f3f3f;
vector<int> jilu[1010];
struct Edge
{
    int v,c,f,nx;
    Edge() {}
    Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],sz;
void init()
{
    sz=0; memset(G,-1,sizeof(G));
}
void add_edge(int u,int v,int c)
{
    E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
    E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
}
bool bfs(int S,int T)
{
    static int Q[MAXN]; memset(dis,-1,sizeof(dis));
    dis[S]=0; Q[0]=S;
    for (int h=0,t=1,u,v,it;h<t;++h)
    {
        for (u=Q[h],it=G[u];~it;it=E[it].nx)
        {
            if (dis[v=E[it].v]==-1&&E[it].c>E[it].f)
            {
                dis[v]=dis[u]+1; Q[t++]=v;
            }
        }
    }
    return dis[T]!=-1;
}
int dfs(int u,int T,int low)
{
    if (u==T) return low;
    int ret=0,tmp,v;
    for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
    {
        if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f)
        {
            if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
            {
                ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
            }
        }
    }
    if (!ret) dis[u]=-1; return ret;
}
int dinic(int S,int T)
{
    int maxflow=0,tmp;
    while (bfs(S,T))
    {
        memcpy(cur,G,sizeof(G));
        while (tmp=dfs(S,T,inf)) maxflow+=tmp;
    }
    return maxflow;
}
int main()
{
    init();
    int m,n;
    scanf("%d%d",&m,&n);
    int s=0,t=n+m+1;
    for(int i=1;i<=m;i++){
        int tmp;
        scanf("%d",&tmp);
        add_edge(s,i,tmp);
    }
    for(int i=1;i<=n;i++){
        int nn;
        scanf("%d",&nn);
        for(int j=0;j<nn;j++){
            int tmp;
            scanf("%d",&tmp);
            add_edge(tmp,i+m,inf);
            for(int k=0;k<jilu[tmp].size();k++){
                add_edge(jilu[tmp][k],i+m,inf);
            }
            jilu[tmp].push_back(i+m);
        }
        scanf("%d",&nn);
        add_edge(i+m,t,nn);
    }
    printf("%d\n",dinic(s,t));
}
时间: 2024-07-29 01:44:16

POJ 1149 PIGS 【网络流】的相关文章

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

POJ 1149 PIGS ( 网络流 )

主要是建图有些小烦,若几个节点流量的来源或者去向完全相同,且流量为 INF,将它们合并成一个节点. 若从两点间有且仅有一条容量为 INF 的边,将两点合并成一个节点. #include <iostream> #include <cstring> #include <queue> using namespace std; const int INF = 0x7fffffff; const int MAXN = 110; int capacity[MAXN][MAXN],

POJ 1149 PIGS 迈克卖猪问题 网络流构图+三种AC方法

题目链接:POJ 1149 PIGS PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16533   Accepted: 7403 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the key

POJ 1149 PIGS 最大流

第一次做网络流,看着教材里面的题解做的= = 用的是Ford,应该是最好理解的把,就是不断的找有没有从源点到汇点的增广路然后更新. 建图真是难啊,而且感觉细节要注意的地方比较多,一开始没有考虑反向弧,WA了两发,sad... #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <strin

POJ 1149 PIGS(最大流)

POJ 1149 PIGS 题目链接 题意:有n个猪圈,m个顾客,猪圈中一开始有一些猪,顾客轮流来(注意是有先后顺序的),然后每个顾客会开启一些猪圈,在开启的猪圈中最多买b只猪,之后可以任意把剩下的猪分配到开着的猪圈中,问最多能卖出几只猪 思路:这题的关键在于建模,由于顾客有先后顺序,假如后来的顾客会开启x门,前面一个顾客也会开启x门,那么前面顾客相当与可以分配给后面顾客, 所以建模的方式为,源点和每个猪圈连,容量为猪圈猪数,每个猪圈和第一个开的顾客连,如果后面有顾客会开这个猪圈,则和之前的顾客

POJ 1149 PIGS(最大流+建图)

题目链接:http://poj.org/problem?id=1149 题意:M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买能打开的猪圈里的猪,而且要买一定数量的猪,每个猪圈有已知数量的猪, 但是猪圈可以重新打开,将猪的个数,重新分配,但是只能将猪往当前打开状态的猪圈里赶,以达到卖出的猪的数量最多. 思路:还是4部分,源点->猪圈->猪圈->汇点 Accepted 976K 63MS C++ 能用EK水的,当然用EK水 #include <iostream> #in

POJ 1149 PIGS(Dinic最大流)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20738   Accepted: 9481 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

网络流(最大流):POJ 1149 PIGS

PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) IO format: %lld      Java class name: Main Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because

POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17598   Accepted: 7977 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t