程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ 1274 The Perfect Stall

POJ 1274 The Perfect Stall

編輯:關於C++

The Perfect Stall

Time Limit : 2000/1000ms (Java/Other)Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 8Accepted 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) .cap-="aug;" aug="(aug==-1?edge[i].cap:min(aug,edge[i].cap));" bool="" flag="false;" i="edge[i].next)" int="" j="0;j<u;j++)" minlevel="NV;" ne="0;" nv="n+m+1;//可能走過的邊的總數" pre="" return="" u="pre[v];" v="=vt)" vs="0;//起點" vt="n+m+1;//終點"><p>
</p>
   
</nv)></int></queue></algorithm></cstring></cstdio></iostream>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved