HPU1179 Ollivanders: Makers of Fine Wands since 382 BC.【二分图最大匹配】

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1179

题目大意:

题目太长了,简单的意思就是:有N个魔杖,M个魔法师,魔杖有多个匹配的魔法师。但是一个魔法师

只能对应一根魔杖。那么问题来了:最多有多少魔法师能得到魔棒。

思路:

做一个二分图,一边是魔杖,另一边是魔法师。相应的匹配作为二分图的边。利用匈牙利算法,求出二

分图最大匹配是多少。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 110;

bool Map[MAXN][MAXN],mask[MAXN];

int N,M,cx[MAXN],cy[MAXN];

int FindPath(int u)
{
    for(int i = 1; i <= M; ++i)
    {
        if(Map[u][i] && !mask[i])
        {
            mask[i] = 1;
            if(cy[i] == -1 || FindPath(cy[i]))
            {
                cy[i] = u;
                cx[u] = i;
                return 1;
            }
        }
    }
    return 0;
}

int MaxMatch()
{
    int ret = 0;
    for(int i = 1; i <= N; ++i)
        cx[i] = -1;
    for(int i = 1; i <= M; ++i)
        cy[i] = -1;

    for(int i = 1; i <= N; ++i)
    {
        if(cx[i] == -1)
        {
            for(int j = 1; j <= M; ++j)
                mask[j] = 0;
            ret += FindPath(i);
        }
    }
    return ret;
}

int main()
{
    int d,w;
    while(~scanf("%d%d",&N,&M))
    {
        memset(Map,false,sizeof(Map));
        for(int i = 1; i <= M; ++i)
        {
            scanf("%d",&d);
            while(d--)
            {
                scanf("%d",&w);
                Map[w][i] = 1;
            }
        }
        printf("%d\n",MaxMatch());
    }

    return 0;
}
时间: 2024-10-03 00:43:01

HPU1179 Ollivanders: Makers of Fine Wands since 382 BC.【二分图最大匹配】的相关文章

HDU1179_Ollivanders: Makers of Fine Wands since 382 BC.(二分图/最大匹配)

解题报告 题意: n个巫师m个魔杖,每个魔杖可以被不同的巫师使用.求多少个魔杖会被买. 思路: 二分图最大匹配简单题. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int mmap[110][110],n,m,vis[110],pre[110]; int dfs(int x) { for(int i=1; i<=n; i++) { if(!vis[i]&

hdu-1179 Ollivanders: Makers of Fine Wands since 382 BC.---二分图匹配模板

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1179 题目大意: 有n个人要去买魔杖,有m根魔杖(和哈利波特去买魔杖的时候一样,是由魔杖选人).接下来是m行,每行第一个数k是第i根魔杖可以选的人数,接着k个数表示这根魔杖选的人的编号.最后问老板最多能卖出多少根魔杖.这个赤裸裸的模版题,套下就OK了. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll;

hdu-----(1179)Ollivanders: Makers of Fine Wands since 382 BC.(二分匹配)

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 935    Accepted Submission(s): 523 Problem Description In Diagon Alley ,there is only one Wand-se

HDU——1179 Ollivanders: Makers of Fine Wands since 382 BC.

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1895    Accepted Submission(s): 1084 Problem Description In Diagon Alley ,there is only one Wand-

HDU 1179 Ollivanders: Makers of Fine Wands since 382 BC.(匈牙利算法)

Problem Description In Diagon Alley ,there is only one Wand-seller,peeling gold letters over the door read Ollivanders: Makers of Fine Wands since 382 BC.A single wand lay on a faded purple cushion in the dusty window. A tinkling bell rang somewhere

HDU 1179 Ollivanders: Makers of Fine Wands since 382 BC.(二分图匹配--匈牙利算法)

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1314 Accepted Submission(s): 718 Problem Description In Diagon Alley ,there is only one Wand-seller

hdu1179——Ollivanders: Makers of Fine Wands since 382 BC.

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 964    Accepted Submission(s): 539 Problem Description In Diagon Alley ,there is only one Wand-s

hdu1179Ollivanders: Makers of Fine Wands since 382 BC. (二分最大匹配)

Problem Description In Diagon Alley ,there is only one Wand-seller,peeling gold letters over the door read Ollivanders: Makers of Fine Wands since 382 BC.A single wand lay on a faded purple cushion in the dusty window. A tinkling bell rang somewhere

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小