• [POJ 2239] Selecting Courses


    [题目链接]

              http://poj.org/problem?id=2239

    [算法]

            将课程作为左部节点,时间作为右部节点,用匈牙利算法求二分图最大匹配即可

    [代码]

            

    #include <algorithm>  
    #include <bitset>  
    #include <cctype>  
    #include <cerrno>  
    #include <clocale>  
    #include <cmath>  
    #include <complex>  
    #include <cstdio>  
    #include <cstdlib>  
    #include <cstring>  
    #include <ctime>  
    #include <deque>  
    #include <exception>  
    #include <fstream>  
    #include <functional>  
    #include <limits>  
    #include <list>  
    #include <map>  
    #include <iomanip>  
    #include <ios>  
    #include <iosfwd>  
    #include <iostream>  
    #include <istream>  
    #include <ostream>  
    #include <queue>  
    #include <set>  
    #include <sstream>  
    #include <stdexcept>  
    #include <streambuf>  
    #include <string>  
    #include <utility>  
    #include <vector>  
    #include <cwchar>  
    #include <cwctype>  
    #include <stack>  
    #include <limits.h>
    using namespace std;
    #define MAXN 350
    const int T = 84;
    
    struct edge
    {
            int to,nxt;
    } e[MAXN * T];
    
    int i,n,m,x,y,ans,tot;
    bool visited[MAXN + T];
    int match[MAXN + T],head[MAXN + T];
    
    inline void addedge(int u,int v)
    {
            tot++;
            e[tot] = (edge){v,head[u]};
            head[u] = tot;
    }
    inline bool hungary(int u)
    {
            int i,v;
            visited[u] = true;
            for (i = head[u]; i; i = e[i].nxt)    
            {
                    v = e[i].to;
                    if (!visited[v])
                    {
                            visited[v] = true;
                            if (!match[v] || hungary(match[v]))
                            {
                                    match[v] = u;
                                    return true;        
                            }    
                    }    
            }    
            return false;
    }
    
    int main() 
    {
            
            while (scanf("%d",&n) != EOF)
            {
                    tot = 0;
                    memset(match,0,sizeof(match));
                    for (i = T + 1; i <= T + n + 1; i++) head[i] = 0;
                    for (i = 1; i <= n; i++)
                    {
                            scanf("%d",&m);
                            while (m--)
                            {
                                    scanf("%d%d",&x,&y);
                                    addedge(i + T,12 * (x - 1) + y);
                            }
                    }
                    ans = 0;
                    for (i = T + 1; i <= T + n + 1; i++)
                    {
                            memset(visited,false,sizeof(visited));
                            if (hungary(i)) ans++;
                    }
                    printf("%d
    ",ans);
            }
            
            return 0;
        
    }
  • 相关阅读:
    centos安装vim
    thrift学习之二----学习资料积累
    thrift学习之一-------介绍
    组合模式
    一致性哈希算法(consistent hashing)
    php配置php-fpm启动参数及配置详解
    error while loading shared libraries的解決方法
    数据结构之二叉树
    768、最多能完成排序的块(贪心算法)
    VS code 配置C++编译环境
  • 原文地址:https://www.cnblogs.com/evenbao/p/9406485.html
Copyright © 2020-2023  润新知