zoukankan      html  css  js  c++  java
  • POJ 1274 The Perfect Stall

    The Perfect Stall

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
    Total Submission(s) : 8   Accepted Submission(s) : 6
    Problem Description
    Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. 
    Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. 
     

    Input
    The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.
     

    Output
    For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
     

    Sample Input
    5 5 2 2 5 3 2 3 4 2 1 5 3 1 2 5 1 2
     

    Sample Output
    4
     

    网络流或者二分匹配。

    题意:  有n头牛  m个摊位  每一个摊位仅仅能容纳一头奶牛 每头牛都有自己愿意产奶的摊位   问最多有几头牛能在愿意的摊位产奶


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define MAXN 44444
    #define MAXM 999999
    #define inf 1<<30
    
    struct Edge
    {
        int v,cap,next;
    } edge[MAXM];
    
    int n,m,vs,vt,NE,NV;
    int head[MAXN];
    
    void Insert(int u,int v,int cap)
    {
        edge[NE].v=v;
        edge[NE].cap=cap;
        edge[NE].next=head[u];
        head[u]=NE++;
    
        edge[NE].v=u;
        edge[NE].cap=0;
        edge[NE].next=head[v];
        head[v]=NE++;
    }
    
    int level[MAXN];
    int gap[MAXN];
    
    void bfs(int vt)
    {
        memset(level,-1,sizeof(level));
        memset(gap,0,sizeof(gap));
        level[vt]=0;
        gap[level[vt]]++;
        queue<int>que;
        que.push(vt);
        while(!que.empty())
        {
            int u=que.front();
            que.pop();
            for(int i=head[u]; i!=-1; i=edge[i].next)
            {
                int v=edge[i].v;
                if(level[v]!=-1)continue;
                level[v]=level[u]+1;
                gap[level[v]]++;
                que.push(v);
    
            }
        }
    }
    
    
    int pre[MAXN];
    int cur[MAXN];
    //參数  起点  终点
    int SAP(int vs,int vt)
    {
        bfs(vt);
        memset(pre,-1,sizeof(pre));
        memcpy(cur,head,sizeof(head));
        int u=pre[vs]=vs,flow=0,aug=inf;
        gap[0]=NV;
        while(level[vs]<NV)
        {
            bool flag=false;
            for(int &i=cur[u]; i!=-1; i=edge[i].next)
            {
                int v=edge[i].v;
                if(edge[i].cap&&level[u]==level[v]+1)
                {
                    flag=true;
                    pre[v]=u;
                    u=v;
                    //  aug=(aug==-1?

    edge[i].cap:min(aug,edge[i].cap)); aug=min(aug,edge[i].cap); if(v==vt) { flow+=aug; for(u=pre[v]; v!=vs; v=u,u=pre[u]) { edge[cur[u]].cap-=aug; edge[cur[u]^1].cap+=aug; } // aug=-1; aug=inf; } break; } } if(flag)continue; int minlevel=NV; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&level[v]<minlevel) { minlevel=level[v]; cur[u]=i; } } if(--gap[level[u]]==0)break; level[u]=minlevel+1; gap[level[u]]++; u=pre[u]; } return flow; } int main() { int u,v,w,w1,w2; while(~scanf("%d%d",&n,&m)) { vs=0;//起点 vt=n+m+1;//终点 NV=n+m+1;//可能走过的边的总数 NE=0; memset(head,-1,sizeof(head)); for(int i=1; i<=n; i++) { //scanf("%d%d",&w1,&w2); Insert(0,i,1); //Insert(i,vt,w2); } for(int i=1; i<=m; i++) { //scanf("%d%d%d",&u,&v,&w); Insert(i+n,n+m+1,1); //Insert(v,u,w); } for(int i=1;i<=n;i++) { scanf("%d",&u); for(int j=0;j<u;j++) { scanf("%d",&v); Insert(i,v+n,1); } } printf("%d ",SAP(vs,vt)); } return 0; }



  • 相关阅读:
    k3 cloud中获取自己开发的单据
    k3 cloud列表中出现很多空白
    k3 cloud支付申请单下推付款单时候提示未将对象引用设置到对象的实例
    k3 cloud出现应收单下推收款单,把收款单是结算方式修改成银行承兑汇票之后保存提示:收款单明细中结算方式为票据业务的实收金额之和不等于票据的当前占用金额之和,请检查数据!
    k3 cloud中出现合计和汇总以后没有显示出来,合价要新增一行以后才出现值
    共享打印机
    k3 cloud总账凭证点击保存的时候提示未将对象引用到对应的实例
    k3 cloud成本调整单引入单据后,再做出库成本核算。成本调整单列表已审核的单据消失,非已审核的单据还在,这是出库成本核算设置参数的问题吗?
    K3 cloud选单时候必须把必录的数据录完以后才可以选单
    k3 cloud成本调整单提示期末余额不存在调整单分录的维度,请先出库核算确认是否存在核算维度的数据
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7049056.html
Copyright © 2011-2022 走看看