CodeChef - RIN Course Selection
Read problems statements in Mandarin Chineseand 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 ithline 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
Input 1:
3 2 2
70 100
100 80
100 90
1 2
1 3 Output 1:
80.00 Input 2:
4 5 4
20 -1 100 -1 -1
100 30 -1 -1 -1
100 -1 30 20 40
100 30 40 50 20
1 2
1 3
2 4
3 4 Output 2:
32.50
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.
类似于HNOI切糕 的建模,首先可以证明最小割一定会割正好N条边,并且每条边都在每个课程的一条链上,所以我们把边权w变成200-w,最后答案就是 200*N-max_flow()。
而强制要求课程的先后顺序,就可以通过链之间连 inf 来实现。
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int maxn=20005;
const int inf=1<<30;
using namespace std;
vector<int> g[maxn];
struct lines{
int to,flow,cap;
}l[maxn*437];
int t=-1,S,T,d[maxn],cur[maxn];
bool v[maxn]; inline void add(int from,int to,int cap){
l[++t]=(lines){to,0,cap},g[from].pb(t);
l[++t]=(lines){from,0,0},g[to].pb(t);
} inline bool BFS(){
queue<int> q;
memset(v,0,sizeof(v));
q.push(S),v[S]=1,d[S]=0;
int x; lines e; while(!q.empty()){
x=q.front(),q.pop();
for(int i=g[x].size()-1;i>=0;i--){
e=l[g[x][i]];
if(e.flow<e.cap&&!v[e.to]){
v[e.to]=1,d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return v[T];
} int dfs(int x,int A){
if(x==T||!A) return A;
int flow=0,f,sz=g[x].size();
for(int &i=cur[x];i<sz;i++){
lines &e=l[g[x][i]];
if(d[x]==d[e.to]-1&&(f=dfs(e.to,min(e.cap-e.flow,A)))){
A-=f,flow+=f;
e.flow+=f,l[g[x][i]^1].flow-=f;
if(!A) break;
}
}
return flow;
} inline int max_flow(){
int an=0;
while(BFS()){
memset(cur,0,sizeof(cur));
an+=dfs(S,1<<30);
}
return an;
} int N,M,K,uu,vv;
int main(){
scanf("%d%d%d",&N,&M,&K),S=0,T=N*(M+1)+1;
for(int i=1;i<=N;i++) add(S,i,inf),add(i+M*N,T,inf);
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++){
scanf("%d",&uu);
if(uu<0) add(i+(j-1)*N,i+j*N,inf);
else add(i+(j-1)*N,i+j*N,200-uu);
}
while(K--){
scanf("%d%d",&uu,&vv);
for(int j=0;j<M;j++) add(uu+j*N,vv+(j+1)*N,inf);
} printf("%.2lf\n",200-max_flow()/(double)N);
return 0;
}
CodeChef - RIN Course Selection的更多相关文章
- Course Selection CodeChef - RIN
All submissions for this problem are available. Read problems statements in Mandarin Chineseand Russ ...
- Codechef RIN 「Codechef14DEC」Course Selection 最小割离散变量模型
问题描述 提供中文版本好评,一直以为 Rin 是题目名字... pdf submit 题解 参考了 东营市胜利第一中学姜志豪 的<网络流的一些建模方法>(2016年信息学奥林匹克中国国家队 ...
- [CODECHEF]RIN
题意:一个人要在$m$个学期上$n$节课,在第$j$学期上$i$课有$X_{i,j}$的收益,有些课$B_i$有前置课程$A_i$,问最大得分 这个题我都做不出来还去看题解...我退役吧== 考虑每种 ...
- CodeChef - RIN 最小割应用 规划问题
题意:给定\(n\)门课和\(m\)个学期,每门课在每个学期有不同的得分,需要选定一个学期去完成,但存在约束条件,共有\(k\)对课程需要\(a\)在\(b\)开始学前学会,求最大得分(原问题是求最高 ...
- [CodeChef]RIN(最小割)
Description 有m门课可以在n个学期内学习,第i门课在第j个学期的收益是\(X_{i,j}\),一个学期可以学多门课,有的课之间有依赖关系,即必须先学a再学b,求最大收益.n,m<= ...
- Codechef Course Selection
Home » Practice(Hard) » Course Selection Course Selection Problem Code: RINSubmit https://www.codech ...
- codechef营养题 第三弹
第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Rac ...
- codechef营养题 第二弹
第二弾が始まる! codechef problems 第二弹 一.Backup Functions 题面 One unavoidable problem with running a restaura ...
- codechef 营养题 第一弹
第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5 ...
随机推荐
- python爬虫基础16-cookie在爬虫中的应用
Cookie的Python爬虫应用 Cookie是什么 Cookie,有时也用其复数形式 Cookies,英文是饼干的意思.指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上 ...
- python模块之pickle
和json不同的是: json只支持str,int,tuple,list,dict. pickle支持python里所有的数据类型,但是只能在python里序列化,不跨平台,python独有. 代码示 ...
- 素数筛选:HDU2710-Max Factor
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- BZOJ 5313: 新Fib数列
打表找规律 #include<cstdio> using namespace std; int F[20]={0,1,1,2,3,0,3,3,1,4,0,4,4,3,2,0,2,2,4,1 ...
- canvas 动画库 CreateJs 之 EaselJS(下篇)
本文来自网易云社区 作者:田亚楠 继承 对应原文:Inheritance 我们可以继承已有的「显示对象」,创建新的自定义类.实现方法有很多种,下面介绍其中之一. 举例:实现一个继承于 Containe ...
- Leetcode4--->求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...
- 2018省赛赛第一次训练题解和ac代码
第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title A CodeForces 607A Chain Reaction B CodeForces ...
- struts拦截器的使用
拦截器的使用 转自http://blog.csdn.net/woshisap/article/details/7271854 1:拦截器(Interceptor) 拦截器是Struts2最强大的特性之 ...
- HashSet源码分析 jdk1.6
Set的特点:Set元素无顺序,且元素不可以重复. 1.定义 public class HashSet<E> extends AbstractSet<E> implements ...
- iOS UICollectionView高级用法(长按自由移动cell)
iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好. @property (nonatomic, strong) UIColle ...