Poj2239-Selecting Courses

Source: Internet
Author: User


View more problem solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410

Reprinted with the source: http://blog.csdn.net/wangjian8006

There are many courses in the university. Now James needs to select a course. He is a learner, so he wants to select as many courses as possible,
There are n courses in the school, and according to the school, there are 12 classes per week every day, so there are 7*12 classes per week.
Enter the first action N, which indicates that there are n courses.
In the next n rows, the first number X in each row indicates that this course needs to be used X times in the week.
Then follow the X pairs of numbers. The first D represents the day of the week, and the second c Represents the class of the day.
If several courses are in Class C that day, you need to select one of them, instead of multiple courses.
Now it is required to calculate the maximum number of courses that James can choose

 

Problem-solving ideas: we will regard this question as a bipartite graph, the left represents the course, the right represents a class, code (D-1) * 12 + C, this means that every lesson of the week is considered as a node.
In this way, if the course corresponds to that class, there will be even one edge. Then, find the maximum matching value for the bipartite graph.

 

/* Memory 232 ktime 32 Ms */# include <iostream> using namespace STD; # define maxv 310int n, m; bool map [maxv] [maxv], use [maxv]; int link [maxv]; bool DFS (int x) {int I, j; for (I = 1; I <= m; I ++) {If (Map [x] [I] &! Use [I]) {use [I] = 1; j = link [I]; Link [I] = x; If (DFS (j) | j =-1) return 1; Link [I] = J;} return 0;} int Hungary () {int I, num = 0; memset (link, -1, sizeof (Link); for (I = 1; I <= N; I ++) {memset (use, 0, sizeof (use )); if (DFS (I) num ++;} return num;} int main () {int I, num, a, B; while (~ Scanf ("% d", & N) {memset (MAP, 0, sizeof (MAP); M = 7*12; for (I = 1; I <= N; I ++) {scanf ("% d", & num); While (Num --) {scanf ("% d", &, & B); map [I] [(A-1) * 12 + B] = 1 ;}} printf ("% d \ n", Hungary ());} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.