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 ...
随机推荐
- hessian应用示例
因为公司的项目远程调用采用的是hessian,故抽时间了解了下hessian,自己也写了一个应用实例,以便加深对hessian的理解. Hessian是一个轻量级的remoting onhttp工具, ...
- 创建Django项目并将其部署在腾讯云上
这段时间在做scrapy爬虫,对爬出来的数据基于Django做了统计与可视化,本想部署在腾讯云上玩玩,但是因为以前没有经验遇到了一些问题,在这里记录一下: 首先说下Django的创建与配置: 1. 创 ...
- Letters CodeForces - 978C (二分)
Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...
- C3P0连接问题
C3P0 连接时的相关问题: 我的环境是在IDEA中使用C3P0中进行的: 使用C3P0主要用到的jar包都是最新和Mysql8.0兼容的包 在连接的时候遇到: 先是在连接的时候出现数据库连接的时候的 ...
- POJ:3041-Asteroids(匈牙利算法模板)
传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...
- Python虚拟机函数机制之无参调用(一)
PyFunctionObject对象 在Python中,任何一个东西都是对象,函数也不例外.函数这种抽象机制,是通过一个Python对象——PyFunctionObject来实现的 typedef s ...
- 小白用shiro(1)
本文来自网易云社区 作者:王飞 首先引入一段关于shiro的介绍: 开发系统中,少不了权限,目前java里的权限框架有SpringSecurity和Shiro(以前叫做jsecurity),对于Spr ...
- jmeter返回的Unicode转换成utf8
该问题通过查找资料借鉴前辈门的经验得到了解决,记录下来是为了后面能够用到 最近发现有些接口返回的时unicode类型的,如下图所示: 因此只需要在jmeter中添加后置处理器:BeanShell Po ...
- Game on Tree
D - Game on Tree Time limit : 2sec / Memory limit : 256MB Score : 1100 points Problem Statement Ther ...
- HDU——2723Electronic Document Security(STL map嵌套set做法)
Electronic Document Security Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...