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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值