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爬虫基础14-selenium大全8/8-常见问题
Selenium笔记(8)常见的坑 本文集链接:https://www.jianshu.com/nb/25338984 用Xpath查找数据时无法直接获取节点属性 通常在我们使用xpath时,可以使用 ...
- python-time模块--pickle模块
目录 time 模块 为什么要有time模块,time模块有什么用? time模块的三种格式 时间戳(timestamp) 格式化时间(需要自己定义格式) 结构化时间(struct-time) 结构化 ...
- python3爬取”理财大视野”中的股票,并分别写入txt、excel和mysql
需求:爬取“理财大视野”网站的排名.代码.名称.市净率.市盈率等信息,并分别写入txt.excel和mysql 环境:python3.6.5 网站:http://www.dashiyetouzi.co ...
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Fight Against Monsters
#include <iostream> #include <cstdio> #include <cstring> #include <string> # ...
- HDU 5236 Article 期望
题意: 你现在要打\(n\)个字符,但是程序随时可能会崩溃. 你可以在恰当的时机按下 \(Ctrl-S\)键,崩溃后,会从最后一次保存的情况继续开始打字. 具体是这样的: 在每个第\(i-0.1s(i ...
- socket中send和recv函数
Socket一次Recv接受的字节有限制么? 从套接字接收数据. 返回值是表示接收数据的字符串. 一次接收的最大数据量由bufsize指定.它默认为零. 注意为了最好地匹配硬件和网络现实,bufsiz ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- python - 字符串的格式化输出
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_2_str.py@ide: PyCharm Community ...
- TOJ4277: Sequence 组合数学
4277: Sequence Time Limit(Common/Java):2000MS/6000MS Memory Limit:65536KByte Total Submit: 39 ...
- C++调用有道翻译API实现在线翻译之发声篇
大概半月前写了一篇博文:C++中使用Curl和JsonCpp调用有道翻译API实现在线翻译, 得到大家的热情捧场,有人看了文章说要是能发声不是更好,我觉得说的也是哈,能听到专家的标准发音,那该是多美的 ...