-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedCoursesAssigments.cpp
More file actions
168 lines (158 loc) · 4.33 KB
/
BalancedCoursesAssigments.cpp
File metadata and controls
168 lines (158 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Problem: Balanced Courses Assignments
Description
At the beginning of the semester, the head of a computer science department D have to
assign courses to teachers in a balanced way. The department D has m teachers T={1,2,...,m}
and n courses C={1,2,...,n}. Each teacher t∈T has a preference list which is a list of
courses he/she can teach depending on his/her specialization. We known a list of pairs of
conflicting two courses that cannot be assigned to the same teacher as these courses have
been already scheduled in the same slot of the timetable. The load of a teacher is the number
of courses assigned to her/him. How to assign nn courses to mm teacher such that each course
assigned to a teacher is in his/her preference list, no two conflicting courses are assigned
to the same teacher, and the maximal load is minimal.
Input
The input consists of following lines
Line 1: contains two integer m and n (1≤m≤10, 1≤n≤30)
Line i+1: contains an positive integer k and k positive integers indicating the courses
that teacher i can teach (∀i=1,…,m)
Line m+2: contains an integer k
Line i+m+2: contains two integer i and j indicating two conflicting courses (∀i=1,…,k)
Output
The output contains a unique number which is the maximal load of the teachers in the solution
found and the value -1 if not solution found.
Example
Input
4 12
5 1 3 5 10 12
5 9 3 4 8 12
6 1 2 3 4 9 7
7 1 2 3 5 6 10 11
25
1 2
1 3
1 5
2 4
2 5
2 6
3 5
3 7
3 10
4 6
4 9
5 6
5 7
5 8
6 8
6 9
7 8
7 10
7 11
8 9
8 11
8 12
9 12
10 11
11 12
Output
3
*/
#include<bits/stdc++.h>
using namespace std;
int m, n;
int maxLoad = INT_MAX; // find min of maxLoad
int referenceCourseOfTeachers[12][32];
int conflictingCourseOfCourse[32][500];
int teacherOfCourse[32];
int load[12];
bool inReferenceCourseOf(int teacherNumb, int courseNumb) {
bool check = false;
for (int i = 1; i <= 30 && referenceCourseOfTeachers[teacherNumb][i] != 0; i++) {
if (referenceCourseOfTeachers[teacherNumb][i] == courseNumb) {
check = true;
break;
}
}
return check;
}
bool conflicted(int teacherNumb, int courseNumb) {
bool check = false;
// get all assigned course of teacher with teacherNumb
vector<int> assignedCourse;
for (int i = 1; i <= courseNumb; i++)
{
if (teacherOfCourse[i] == teacherNumb) {assignedCourse.push_back(i);}
}
for (unsigned int i = 0; i < assignedCourse.size(); i++)
{
int c = assignedCourse[i];
for (int j = 0; j < 436 && conflictingCourseOfCourse[c][j] != 0; j++)
{
if (conflictingCourseOfCourse[c][j] == courseNumb) {
check = true;
break;
}
}
if (check == true) break;
}
return check;
}
bool isCanAssignTeacher(int course, int teacher) {
if (!inReferenceCourseOf(teacher, course)) return false;
else if (conflicted(teacher, course)) return false;
return true;
}
void printSol() {
for (int i = 1; i <= n; i++)
{
printf("%d ", teacherOfCourse[i]);
}
printf("\n");
}
void Try(int course) {
for (int teacher = 1; teacher <= m; teacher++)
{
if (isCanAssignTeacher(course, teacher)) {
teacherOfCourse[course] = teacher;
load[teacher]++;
if (course == n)
{
int maxLoadOfCurrentSolution = -INT_MAX; // find max
for(int i = 1; i <= m; i++) {maxLoadOfCurrentSolution = max(maxLoadOfCurrentSolution, load[i]);}
maxLoad = min(maxLoad, maxLoadOfCurrentSolution);
// printSol();
} else if(load[teacher] < maxLoad) Try(course+1);
load[teacher]--;
}
}
}
int main() {
cin >> m >> n;
int k;
for (int i = 1; i <= m; i++)
{
cin >> k;
for (int j = 1; j <= k; j++)
{
cin >> referenceCourseOfTeachers[i][j];
}
}
cin >> k;
int a, b;
for (int i = 0; i < k; i++)
{
cin >> a >> b;
int len_a = 0;
while (conflictingCourseOfCourse[a][len_a] != 0) {
len_a++;
}
conflictingCourseOfCourse[a][len_a] = b;
int len_b = 0;
while (conflictingCourseOfCourse[b][len_b] != 0) {
len_b++;
}
conflictingCourseOfCourse[b][len_b] = a;
}
Try(1);
cout << maxLoad << endl;
return 0;
}