zoukankan      html  css  js  c++  java
  • BZOJ 1191: [HNOI2006]超级英雄Hero 匈牙利算法

    1191: [HNOI2006]超级英雄Hero

    Time Limit: 2 Sec  Memory Limit: 256 MB
    Submit: xxx  Solved: 2xx

    题目连接

    http://www.lydsy.com/JudgeOnline/problem.php?id=1191

    Description

    现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问 题,然后根据回答问题的多少获得不同数目的奖品或奖金。主持人问题准备了若干道题目,只有当选手正确回答一道题后,才能进入下一题,否则就被淘汰。为了增 加节目的趣味性并适当降低难度,主持人总提供给选手几个“锦囊妙计”,比如求助现场观众,或者去掉若干个错误答案(选择题)等等。 这里,我们把规则稍微改变一下。假设主持人总共有m道题,选手有n种不同的“锦囊妙计”。主持人规定,每道题都可以从两种“锦囊妙计”中选择一种,而每种 “锦囊妙计”只能用一次。我们又假设一道题使用了它允许的锦囊妙计后,就一定能正确回答,顺利进入下一题。现在我来到了节目现场,可是我实在是太笨了,以 至于一道题也不会做,每道题只好借助使用“锦囊妙计”来通过。如果我事先就知道了每道题能够使用哪两种“锦囊妙计”,那么你能告诉我怎样选择才能通过最多 的题数吗?

    Input

    输入文件的一行是两个正整数n和m(0 < n <1001,0 < m < 1001)表示总共有n中“锦囊妙计”,编号为0~n-1,总共有m个问题。
    以下的m行,每行两个数,分别表示第m个问题可以使用的“锦囊妙计”的编号。
    注意,每种编号的“锦囊妙计”只能使用一次,同一个问题的两个“锦囊妙计”可能一样。

    Output

    第一行为最多能通过的题数p

    Sample Input

    5 6
    3 2
    2 0
    0 3
    0 4
    3 2
    3 2

    Sample Output

    4

    HINT

    题解:

    对每道题,题目和锦囊连边,然后跑一发匈牙利算法,如果不能匹配,那就直接break就好啦

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 2001
    #define mod 10007
    #define eps 1e-9
    //const int inf=0x7fffffff;   //无限大
    const int inf=0x3f3f3f3f;
    /*
    
    */
    //**************************************************************************************
    
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int ma[maxn][maxn];
    int vis[maxn];
    int match[maxn];
    int n,m;
    int dfs(int a)
    {
        for(int i=0;i<n;i++)
        {
            if(ma[a][i]==1&&vis[i]==0)
            {
                vis[i]=1;
                if(match[i]==-1||dfs(match[i]))
                {
                    match[i]=a;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        memset(match,-1,sizeof(match));
        n=read(),m=read();
        for(int i=0;i<m;i++)
        {
            int x=read(),y=read();
            ma[i][x]=ma[i][y]=1;
        }
        int ans=0;
        for(int i=0;i<m;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i)==1)
                ans++;
            else
                break;
        }
        printf("%d
    ",ans);
    }
  • 相关阅读:
    Python入门系列——第20篇
    Python入门系列——第19篇
    windows下python使用pip命令安装builtwith库时,遇到的utf-8问题的解决
    Python入门系列——第18篇
    在相同的主机上创建一个duplicate数据库
    duplicate database的时候,rman连接 auxiliary database的后状态不正确
    使用duplicate target database ... from active database复制数据库
    RHEL7
    Oracle 监听器日志文件过大导致监听异常
    TNS-01251: Cannot set trace/log directory under ADR
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4369123.html
Copyright © 2011-2022 走看看