POJ2239 Selecting Courses【二部图最大匹配】

主题链接:

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

题目大意:

学校总共同拥有N门课程,而且学校规定每天上12节可,一周上7天。

给你每门课每周上的次数,和哪一天哪一节

课上的。假设有多门课程在同一天同一节课上。那么你仅仅能选择当中一门。那么问题来了:最多能同一时候选多少

门课而不发生冲突呢。

输入说明:

先给你一个N。表示有N门课。接下来N行,每行第一个数字x,表示这门课每周上几节。接下来是x对数。第

一个数D表示是这一周哪一天上的,第二个数C表示是这一天哪一节课上的。

思路:

将这道题来看做二分图匹配问题。

建立一个二分图,一边代表课程,一边代表某一节课(将一周7*12节课按编

号1~7*12来排列)。将课程和该课程上的某一节课相应建边,再求这个二分图的最大匹配数就可以。这里用匈牙

利DFS版来做。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 330;

int Map[MAXN][MAXN];
int Mask[MAXN];
int N,M;
int cx[MAXN],cy[MAXN];

int FindPath(int u)
{
    for(int i = 1; i <= M; ++i)
    {
        if(Map[u][i] && !Mask[i])
        {
            Mask[i] = 1;
            if(cy[i] == -1 || FindPath(cy[i]))
            {
                cy[i] = u;
                cx[u] = i;
                return 1;
            }
        }
    }
    return 0;
}

int MaxMatch()
{
    int res = 0;
    for(int i = 1; i <= N; ++i)
        cx[i] = -1;
    for(int i = 1; i <= M; ++i)
        cy[i] = -1;

    for(int i = 1; i <= N; ++i)
    {
        if(cx[i] == -1)
        {
            for(int j = 1; j <= M; ++j)
                Mask[j] = 0;
            res += FindPath(i);
        }
    }
    return res;
}

int main()
{
    int a,b,id;
    while(~scanf("%d",&N))
    {
        memset(Map,false,sizeof(Map));
        M = 7*12;

        for(int i = 1; i <= N; ++i)
        {
            scanf("%d",&id);
            for(int j = 1; j <= id; ++j)
            {
                scanf("%d%d",&a,&b);
                Map[i][(a-1)*12+b] = 1;
            }
        }
        printf("%d\n",MaxMatch());
    }

    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-08-11 05:43:50

POJ2239 Selecting Courses【二部图最大匹配】的相关文章

poj2239 Selecting Courses --- 二分图最大匹配

匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include

POJ 2239 Selecting Courses【最大匹配】

大意: 有一些课程,每个课程的上课时间在每周可能会有多节,你可以从中任选一个时间去听课, 每周七天一天12节课  告诉你每节课的上课时间问在不冲突的情况下最多上几节课? 分析: 左集合课程 右集合时间 边为该节课对应的上课时间 求最大匹配就行了 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std

POJ2239 Selecting Courses(二分图最大匹配)

题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 using namespa

POJ2239 Selecting Courses【二分图最大匹配】

题目链接: http://poj.org/problem?id=2239 题目大意: 学校总共有N门课程,并且学校规定每天上12节可,一周上7天.给你每门课每周上的次数,和哪一天哪一节 课上的.如果有多门课程在同一天同一节课上,那么你只能选择其中一门.那么问题来了:最多能同时选多少 门课而不发生冲突呢. 输入说明: 先给你一个N,表示有N门课.接下来N行,每行第一个数字x,表示这门课每周上几节.接下来是x对数.第 一个数D表示是这一周哪一天上的,第二个数C表示是这一天哪一节课上的. 思路: 将这

[POJ] 2239 Selecting Courses(二分图最大匹配)

题目地址:http://poj.org/problem?id=2239 Li Ming大学选课,每天12节课,每周7天,每种同样的课可能有多节分布在不同天的不同节.问Li Ming最多可以选多少节课.把n种课划分为X集合,把一周的84节课划分为Y集合, 从Xi向Yi连边,那么就转化成了求二分图的最大匹配数,然后匈牙利算法就可以了. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #includ

POJ 2239 Selecting Courses(二分匹配)

题目链接:http://poj.org/problem?id=2239 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning

Poj 2239 Selecting Courses 【二分匹配】

Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9007 Accepted: 4010 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming i

Hdoj 3697 Selecting courses 【贪心】

Selecting courses Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 2082    Accepted Submission(s): 543 Problem Description A new Semester is coming and students are troubling for selecting cours

hdu 3697 10 福州 现场 H - Selecting courses

Description A new Semester is coming and students are troubling for selecting courses. Students select their course on the web course system. There are n courses, the ith course is available during the time interval (A i,B i). That means, if you want