zoukankan      html  css  js  c++  java
  • poj1469 二分图匹配(匈牙利算法)

    poj1469 course

    code:

    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define P 110
    #define N 310
    int map[P][N];
    int match[N];
    bool use[N];
    int p, n;
    
    bool find(int u) //u是课程
    {
        for(int i = 1; i <= n; ++i)
        {
            if(!use[i] && map[u][i]) //标记匹配的学生
            {
                use[i] = true;
                if(match[i] == - 1 || find(match[i]))
                {
                    match[i] = u;
                    return true;
                }
            }
        }
        return false;
    }
    
    int sum()
    {
        int sumall = 0;
        for(int i = 1; i <= p; ++i) //统计匹配的课程
        {
            memset(use, false, sizeof(use));
            if(find(i))
                sumall++;
        }
        return sumall;
    }
    
    int main()
    {
        int ncase;
        int stunum, temp, ans;
        scanf("%d", &ncase);
        while(ncase--)
        {
            memset(map, 0, sizeof(map));
            memset(match, -1, sizeof(match));
            scanf("%d%d", &p, &n);
            for(int i = 1; i <= p; ++i)
            {
                scanf("%d", &stunum);
                for(int j = 1; j <= stunum; ++j)
                {
                    scanf("%d", &temp);
                    map[i][temp] = 1; //j号学生喜欢i号课程
                }
            }
            ans = sum();
            printf("%s
    ", ans == p ? "YES" : "NO");
        }
        return 0;
    }
  • 相关阅读:
    Servlet核心技术(上)
    Bootstrap详解
    ECMAScript6详解
    JQuery详解
    CSS详解
    HTML
    网站加载页面(HTML+CSS+JS,简易版)
    java中sort()方法的用法
    Maven常见jar包依赖
    解决idea的项目启动报404的问题
  • 原文地址:https://www.cnblogs.com/zhangjialu2015/p/5668598.html
Copyright © 2011-2022 走看看