poj 1274 The Perfact Stall

简介:

click here ~~

                                   ***The Perfect Stall***


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面墙,每头牛有自己喜欢的墙,要求每堵墙只能有一头牛,求最多的匹配数

解题思路:,题目要求最大的匹配数,也就是通过左边的点,从右边的点穿出,使得穿出的个数最多,每 个点只能穿过一次,我们在图中加个源点和汇点就行了。。。然后,有了源点和汇点,源点到左边每个的流量都是 1,也就是只能通过 1 次,汇点也类似,而左边的点到右 的点对应的边的边容量为 1,就这样,这道题成功转换为最大流问题,建图后最大流解决

具体看代码:

/*
Date : 2015-8-21 晚上

Author : ITAK

Motto :

今日的我要超越昨日的我,明日的我要胜过今日的我;
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>

using namespace std;
///oo表示无穷大
const int oo = 1e9+5;
///mm表示边的最大数量,因为要双向建边
const int mm = 111111;
///点的最大数量
const int mn = 1000;
///node:节点数,src:源点,dest:汇点,edge:边数
int node, src, dest, edge;
///ver:边指向的结点,flow:边的流量,next:链表的下一条边
int ver[mm], flow[mm], next[mm];
///head:节点的链表头,work:用于算法中的临时链表头,dis:距离
int head[mn], work[mn], dis[mn], q[mn];

///初始化
void Init(int _node, int _src, int _dest)
{
    node = _node, src = _src, dest = _dest;
    for(int i=0; i<node; i++)
        head[i] = -1;
    edge = 0;
}

///增加边
void addedge(int u, int v, int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}

///广搜计算出每个点与源点的最短距离,如果不能到达汇点说明算法结束
bool Dinic_bfs()
{
    int i, u, v, l, r = 0;
    for(i=0; i<node; i++)
        dis[i] = -1;
    dis[q[r++]=src] = 0;
    for(l=0; l<r; l++)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i] && dis[v=ver[i]]<0)
            {
                ///这条边必须有剩余流量
                dis[q[r++]=v] = dis[u] + 1;
                if(v == dest)
                    return 1;
            }
    return 0;
}

///寻找可行流的增广路算法,按节点的距离来找,加快速度
int Dinic_dfs(int u, int exp)
{
    if(u == dest)
        return exp;
    ///work 是临时链表头,这里用 i 引用它,这样寻找过的边不再寻找*
    for(int &i=work[u],v,tmp; i>=0; i=next[i])
    {
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            ///正反向边容量改变
            flow[i] -= tmp;
            flow[i^1] += tmp;
            return tmp;
        }
    }

    return 0;
}

///求最大流,直到没有可行流
int Dinic_flow()
{
    int i, ret=0, data;
    while(Dinic_bfs())
    {
        for(i=0; i<node; i++)
            work[i] = head[i];
        while(data = Dinic_dfs(src, oo))
            ret += data;//cout<<666<<endl;
    }

    return ret;
}
int main()
{
    int n, m, u, v, c;
    while(cin>>n>>m)
    {
        Init(n+m+2, 0, n+m+1);
        for(u=1; u<=n; u++)
        {
            addedge(src, u, 1);
            cin>>c;
            while(c--)
            {
                cin>>v;
                addedge(u, v+n, 1);
            }
        }
        while(m)
            addedge(n+m--, dest, 1);
        cout<<Dinic_flow()<<endl;
    }
    return 0;
}
目录
相关文章
|
人工智能 算法 BI
poj 2192 Zipper
题目链接:http://poj.org/problem?id=2192 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18658   Accepted: 6651 Description Given ...
951 0
|
测试技术
POJ 1001
此题用最朴素的思路实现即可,需模拟加法器,乘法器,最烦人的地方是特殊情形,如末位是小数点(12.^2=144,取小数点),整数末位是0(100^2=10000),0次幂,测试用例可能超出题目中说的范围,可能包含0次幂(100.0^0=0, 0.10^1=0.1)。
729 0
poj 3664
http://poj.org/problem?id=3664 进行两轮选举,第一轮选前n进入第二轮,第二轮选最高   #include #include using namespace std; struct vote { int a,b; int c; ...
705 0
|
人工智能 BI
poj-1008-玛雅历
Description 上周末,M.A. Ya教授对古老的玛雅有了一个重大发现。从一个古老的节绳(玛雅人用于记事的工具)中,教授发现玛雅人使用了一个一年有365天的叫做Haab的历法。这个Haab历法拥有19个月,在开始的18个月,一个月有20天,月份的名字分别是pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu。
854 0
|
消息中间件 人工智能 JavaScript