Majestic Gourmet University
Time limit2sMemory limit512 MB
Given proposed FC and IC lab slots with teacher conflicts, seat limits, and timing rules, choose a valid set of labs using the fewest distinct starting days.
- Level
Hard9 of 10
- Topics
- Graph, BFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
Majestic Gourmet University awards the Majestic International Cooking Degree to a large number of students every year.
Every student takes two mandatory classes, French Cuisine (FC) and Italian Cuisine (IC). Two disjoint groups of teachers run them, the FC teachers and the IC teachers. Both classes run as weekly cooking labs and have a limited number of seats, students per FC lab and students per IC lab. Several lab instances of each class therefore run every week, and each instance has its own weekly timeslot and its own assigned teacher. One more complication exists. Some of the teachers are in open conflict with each other because they disagree on teaching methods.
Every year the heads of the FC department and the IC department propose a number of weekly lab instances, and Bob from the Planning Office picks a subset of these propositions so that every student gets a valid timetable, which means:
- Every student attends exactly one weekly FC lab and one weekly IC lab. A student cannot attend two labs whose timeslots overlap, and cannot attend two labs separated by less than 5 minutes, because switching classrooms takes time.
- The seat limits hold: at most students in an FC lab, and at most students in an IC lab.
- If FC teacher A is in conflict with IC teacher B, then no student of a lab taught by A attends a lab taught by B.
- Bob also wants as many free days as possible. Among all valid choices, the starting times of the picked labs must fall on the fewest possible days of the week. Those days do not have to be consecutive.
Timeslots are compared over the whole week. A lab that starts on day at hours minutes runs from minute to minute , where is the duration of that class in hours. A student can attend two labs exactly when one of them starts at least 5 minutes after the other one ends.
Some teachers may take no lab instance at all, and one teacher may be in charge of several labs.
Help Bob and report the smallest possible number of days.
Input
Every line holds integers separated by single spaces.
The first line holds , the number of registered students.
The second line holds , , and : the number of FC lab propositions, the seat limit of every FC lab, the duration in hours of every FC lab, and the number of FC teachers.
Each of the next lines describes one proposed FC lab slot with four integers , , and : the day of the week, the starting hour, the starting minute, and the FC teacher in charge.
The next line holds , , and with the same meanings for IC.
Each of the next lines describes one proposed IC lab slot in the same format.
The next line holds , the number of conflicts among teachers.
Each of the last lines holds two integers and , meaning that FC teacher is in conflict with IC teacher .
The limits are:
- for every lab slot
- and for every lab slot
- for an FC slot, and for an IC slot
- and for every conflict
Two propositions for the same teacher never overlap.
Output
Print one integer on a single line. Print 0 if no valid choice exists. Otherwise print the smallest number of days of the week that carry the starting times of the picked lab slots.