• poj2239-Selecting Courses(匈牙利算法)


    题目链接

    题目大意

     李明在学校选课,每组数据的第一行为n,代表可选课程的数目,然后接下来n行为课程的时间描述,每行开头一个数t代表该课程在一周内的开课次数,然后每次两个数p, q代表在星期几第几课,然后要求算出李明怎么样才能选尽可能多的课并且不会出现冲突.(真是好学生啊!!!!)

    思路

     此题就是匈牙利算法求最大匹配数的模板题;

    code

    #include <iostream>
    #include <cstring>
    #include <vector>
    using namespace std;
    const int MAX = 300+5;
    
    int checkY[MAX];
    int match[MAX];             
    vector<vector<int> > e(MAX);
    
    bool find(int v) {
        for(size_t i = 0; i < e[v].size(); ++ i) {
            int to = e[v][i];
            if(!checkY[to]) {
                checkY[to] = 1;
                if(match[to] == -1 || find(match[to])) {
                    match[to] = v;
                    return true;
                }
            }
        }
        return false;
    }
    
    int hungary(int n) {
        int cnt = 0;
        memset(match, -1, sizeof(match));
        for(int i = 0; i < n; ++ i) {
            memset(checkY, 0, sizeof(checkY));
            cnt += find(i);
        }
        return cnt;
    }
    
    int main() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        //ifstream cin("data.in");
        int n;
        while(cin >> n) {
            for(int i = 0; i < n; i ++) {
                e[i].clear();
                int t, p, q;
                cin >> t;
                while(t --) {
                    cin >> p >> q;
                    e[i].push_back((p-1)*12 + q);
                }
            }
            cout << hungary(n) << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    软件公司项目经理岗位职责
    指针和链表
    数据结构
    五子棋
    AtCoder Grand Contest 031 B
    两道dp
    博客搬迁
    [Codeforces Round #526 (Div. 2)]
    [Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition]
    [codeforces Mail.Ru Cup 2018 Round 3][B Divide Candies ][思维+数学]
  • 原文地址:https://www.cnblogs.com/topk/p/6580073.html
Copyright © 2020-2023  润新知