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;
}

  

原文地址:https://www.cnblogs.com/topk/p/6580073.html