Course Selection CodeChef - RIN
All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Rin is attending a university.
She has M semesters to finish her program, and that program has N required courses. Each course must be taken in exactly one of the semesters.
Some courses have prerequisites: for each i from 1 to K, she must take course A[i]before course B[i].
The same course may be taught by different professors in different semesters, and how well she does will depend on which professor teaches her. Additionally, some courses may not be taught every semester.
We are given an array X representing this information. For each course i and each semester j, X[i][j] = -1 if course i is not taught in semester j. Otherwise, X[i][j] will be an integer between 0 and 100: the expected score Rin will get if she takes course i in semester j.
Rin may take any number of courses per semester, including none, as long as they are taught that semester and she has already taken any required prerequisite courses.
Help Rin to find the maximal average score she can get over her whole program.
Input
The first line contain 3 integers: N, M and K.
This is followed by N lines, each containing M integers. The jth integer on the ith line represents the value of X[i][j].
This is followed by K lines, each containing two integers: A[i] and B[i].
Output
Output one real number: the maximal average score, rounded to 2 digits after the decimal point.
Constraints
- 1 ≤ M, N ≤ 100
- 0 ≤ K ≤ 100
- -1 ≤ X[i][j] ≤ 100
- 1 ≤ A[i], B[i] ≤ N
- For each i, A[i] ≠ B[i].
- For different i and j, (A[i], B[i]) ≠ (A[j], B[j]).
- We guarantee there exists a way to take these N courses in M semesters.
Subtasks
Subtask 1: (20 Points) A course can have at most 1 pre request course.
Subtask 2: (80 Points) Refer to constraints above
Example
Explanation
Example case 1
The only way she can finish these 3 courses is: take course 1 in the first semester, then take courses 2 and 3 in the second semester. The average score is (70 + 80 + 90) / 3 = 80.00.
Example case 2
The optimal solution is: take course 1 in semester 1, course 2 in semester 2, course 3 in semester 3 and course 4 in semester 4.
1|0EXPLANATION:
First, let's ignore the dependencies. The answer is just the maximum grade per course in any of the semesters, which is trivial to compute but let's put this in another perspective:
For each course, the best grade is 100 - (minimum grade we lose by picking one of the semesters).
To model this as a network flow graph, we do 4 things:
- Create a vertex for each pair (course i, semester j).
- Create a source vertex which is connected to the first semester of each course i by an edge with capacity 100 - grade(i, 1).
- For every semester j from 2 to M, connect the vertices of each course i from semester j-1 to j with capacity 100 - grade(i, j) or 100 if the course is not available (it's the same as having grade 0, recall that it's guaranteed there is a solution).
- Create a sink vertex and connect the last semester of each course to it, with infinite capacity.
Consider the following example with 3 courses and 3 semesters:
The corresponding graph is depicted in the above picture. The maximum flow is equal to the combined grade loss for all courses. We pick semester 3 for course 1 (zero loss), and semester 1 for courses 2 and 3 (loss 20+20). The maximum grade average we can get is (N * 100 - maxflow) / N. In this case: (3*100 - 40) / 3 ~= 86.67.
- Returning to the problem, why does this help?
If we model the problem this way, we can also include the course dependencies. Suppose there are dependencies 1->2 and 1->3. The best we can do is course 1 in semester 2 and courses 2 and 3 in semester 3 for (70+40+40)/3 average.
- If there are dependencies 1->2 and 1->3, then courses 2 and 3 can never be done in the first semester.
This implies that the minimum grade loss for courses 2 and 3 is not bounded by its grades in semester 1. This is the same as changing the capacities associated to semester 1 of courses 2 and 3 to infinity.
- Why do we pick semester j for course 1?
The combined grade loss of doing course 1 in semester 2 + courses 2 and 3 in semester 3 is less than doing course 1 in semester 1 + courses 2 and 3 in semesters 2 or 3.
In terms of network flow, this means that
- if we pick semester j = 1, then courses 2 and 3 are not bounded by grade loss in semester 1. To combine the grade loss by picking semester 1, we connect vertex (course 1, semester 1) to (course 2, semester 2) and (course 3, semester 2) with infinite capacity.
- if we pick semester j = 2, then courses 2 and 3 are not bounded by grade loss in semesters 1 and 2. Same as above but connecting (course 1, semester 2) to courses 2 and 3, semester 3.
- picking semester j = 3 is not possible.
The resulting network is the following
We can see that the maximum flow is 150. The best is to distribute the grade loss of course 1 in semester 2, flow 3, among courses 2 and 3 in semester 3. In fact, 70+40+40 = 300 - 150.
To give you another example, consider the input
The best we can do course 1 at semester 1 (10) + course 2 at semester 2 (90) and course 3 at semester 3 (70) = 10+90+70. The resulting graph is
We can see that the maximum flow is 130 because in this case, it is better to distribute the grade loss of course 1 at semester 1, the flow 90, among the grade loss of courses 2 and 3 over semesters 2 and 3, respectively.
In a nutshell, we create a graph with N*M+2 vertices and use a standard maximum flow algorithm. The hard part was to realize how to build the network correctly.
Proof sketch:
Since max-flow equals min-cut, we can think of our problem as of finding a minimum cut in the above graph. Let Pi be a path corresponding to i-th course, i.e. the path formed of edges corresponding to i-th course in all semesters. In order to prove the correction of the above construction, we can show two properties of the graph:
- For every 1 <= i <= N, min-cut contains exactly one edge from Pi.
- For every constraint (i, j), if min-cut contains the k-th edge of Pi, then it contains the x-th edge of Pj, where x > k i.e. j-th course is taken in later semester than i-th course.
These two fact can be shown quite easily, but we will omit exact proofs here. Intuitively, if you pick any edge from Pi, then Pi is disconnected and there is no need for taking any other edge from it. Moreover, an edge (i, j) corresponding to a constraint, prevent us of taking course j before course i, because if you did it, then in order to make the graph disconnected, you would have to add the second edge of Pj to min-cut which contradicts the first fact.
__EOF__

本文链接:https://www.cnblogs.com/shenben/p/6624196.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术