Home » Practice(Hard) » Course Selection

Course Selection Problem Code: RINSubmit

https://www.codechef.com/problems/RIN

All submissions for this problem are available.

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 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

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.

n个课程要在m个学期里完成,

有k个依赖关系a,b ,表示a课程必须在b课程之前

每个课程在每个学期有相应得分

-1 表示本学期不开设此课程

问 最大的课程得分平均值

最大得分转化为最小扣分

对下面这张图跑最小割,ans=n*100-最小割

解释:       Step 4 :  inf边一定不会被割,a是b的前置课程,如果b的边割在了a之前,图可以通过inf边联通,不符合最小割

求助大佬,为什么以下建图方式不对

#include<queue>
#include<cstdio>
#include<algorithm>
#define inf 0x7fffffff
#define N 10101
#define M 50000
using namespace std;
queue<int>q;
int n,m,k,src,decc,tmp;
int front[N],to[M],nxt[M],tot=;
int lev[N],cur[N],cap[M];
int score[][];
bool f[];
//#define turn((i),(j)) ((i)-1)*(m+1)+(j)
int turn(int i,int j)
{
return (i-)*m+j;
}
void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=w;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
}
bool bfs()
{
while(!q.empty()) q.pop();
for(int i=src;i<=decc;i++) cur[i]=front[i],lev[i]=-;
lev[src]=; q.push(src);
int now;
while(!q.empty())
{
now=q.front(); q.pop();
for(int i=front[now];i;i=nxt[i])
{
if(lev[to[i]]==-&&cap[i]>)
{
lev[to[i]]=lev[now]+;
if(to[i]==decc) return true;
q.push(to[i]);
}
}
}
return false;
}
int dinic(int now,int flow)
{
if(now==decc) return flow;
int rest=,delta;
for(int &i=cur[now];i;i=nxt[i])
{
if(lev[to[i]]>lev[now]&&cap[i]>)
{
delta=dinic(to[i],min(flow-rest,cap[i]));
if(delta)
{
cap[i]-=delta; cap[i^]+=delta;
rest+=delta; if(rest==flow) break;
}
}
}
if(rest!=flow) lev[now]=-;
return rest;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
int x; decc=n*m+;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&score[i][j]);
for(int i=;i<=n;i++) add(turn(i,m),decc,inf);
int a,b;
while(k--)
{
scanf("%d%d",&a,&b);
for(int i=;i<m;i++) add(turn(a,i),turn(b,i+),inf);
f[b]=true;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(j==)
{
if(f[i]) add(src,turn(i,),inf);
else add(src,turn(i,),score[i][j]==- ? inf : -score[i][j]);
}
else add(turn(i,j-),turn(i,j),score[i][j]==- ? inf : -score[i][j]);
while(bfs())
tmp+=dinic(src,inf);
tmp=n*-tmp;
printf("%.2lf",double(tmp)/n);
}

错误建图代码

#include<queue>
#include<cstdio>
#include<algorithm>
#define inf 0x7fffffff
#define N 10101
#define M 50000
using namespace std;
queue<int>q;
int n,m,k,src,decc,tmp;
int front[N],to[M],nxt[M],tot=;
int lev[N],cur[N],cap[M];
//#define turn((i),(j)) ((i)-1)*(m+1)+(j)
int turn(int i,int j)
{
return (i-)*(m+)+j;
}
void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=w;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
}
bool bfs()
{
while(!q.empty()) q.pop();
for(int i=src;i<=decc;i++) cur[i]=front[i],lev[i]=-;
lev[src]=; q.push(src);
int now;
while(!q.empty())
{
now=q.front(); q.pop();
for(int i=front[now];i;i=nxt[i])
{
if(lev[to[i]]==-&&cap[i]>)
{
lev[to[i]]=lev[now]+;
if(to[i]==decc) return true;
q.push(to[i]);
}
}
}
return false;
}
int dinic(int now,int flow)
{
if(now==decc) return flow;
int rest=,delta;
for(int &i=cur[now];i;i=nxt[i])
{
if(lev[to[i]]>lev[now]&&cap[i]>)
{
delta=dinic(to[i],min(flow-rest,cap[i]));
if(delta)
{
cap[i]-=delta; cap[i^]+=delta;
rest+=delta; if(rest==flow) break;
}
}
}
if(rest!=flow) lev[now]=-;
return rest;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
int x; decc=n*(m+)+;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&x);
if(x==-) x=inf;
else x=-x;
add(turn(i,j),turn(i,j+),x);
}
for(int i=;i<=n;i++)
add(,turn(i,),inf),
add(turn(i,m+),decc,inf);
int a,b;
while(k--)
{
scanf("%d%d",&a,&b);
for(int i=;i<=m;i++) add(turn(a,i),turn(b,i+),inf);
}
while(bfs())
tmp+=dinic(src,inf);
tmp=n*-tmp;
printf("%.2lf",double(tmp)/n);
}

Codechef Course Selection的更多相关文章

  1. Course Selection CodeChef - RIN

    All submissions for this problem are available. Read problems statements in Mandarin Chineseand Russ ...

  2. CodeChef - RIN Course Selection

    Read problems statements in Mandarin Chineseand Russian. Rin is attending a university. She has M se ...

  3. Codechef RIN 「Codechef14DEC」Course Selection 最小割离散变量模型

    问题描述 提供中文版本好评,一直以为 Rin 是题目名字... pdf submit 题解 参考了 东营市胜利第一中学姜志豪 的<网络流的一些建模方法>(2016年信息学奥林匹克中国国家队 ...

  4. oncopy="document.selection.empty()"跟oncopy="return false"什么区别?

    实现效果一样,禁止复制. 区别: oncopy="document.selection.empty()"  没禁止,只是把它复制的内容,变成空了: oncopy="ret ...

  5. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  6. D3中selection之使用

    1. 极为重要的reference: [1] How selections works. http://bost.ocks.org/mike/selection/ [2] Nested selecti ...

  7. CSS的::selection使用方法

    请选择本页面文本看看:http://hovertree.com/h/bjaf/38hq6y9d.htm CSS改变默认文本选中的颜色的方法 一般情况下在网页里的文本我们用鼠标选中的时候都是蓝色的,这个 ...

  8. 浏览器Range,Selection等选中文本对象

    Range对象 Range 对象表示文档的连续范围区域,如用户在浏览器窗口中用鼠标拖动选中的区域. 最常见的Range是用户文本选择范围(user text selection).当用户选择了页面上的 ...

  9. 【转】[特征选择] An Introduction to Feature Selection 翻译

    中文原文链接:http://www.cnblogs.com/AHappyCat/p/5318042.html 英文原文链接: An Introduction to Feature Selection ...

随机推荐

  1. 欢迎来怼—第三次Scrum会议

    一.会议成员 队名:欢迎来怼队长:田继平队员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片: 二.会议时间 2017年10月15日    17:15-17:41   总用时26min 三.会议地点 ...

  2. RIGHT-BICEP测试第二次

    1.Right-结果是否正确? 正确 2.B-是否所有的边界条件都是正确的? 正确 3.P-是否满足性能要求? 部分满足 4.是否满足有无括号? 无 5.数字个数是否不超过十? 只是双目运算 6.能否 ...

  3. 02慕课网《进击Node.js基础(一)》——CommonJs标准

    是一套规范管理模块 每个js 为一个模块,多个模块作为一个包 node.js和Couchdb是对其的实现: 不同于jQuery 模块:定义.标识.引用(地址/模块名称) 模块类型: 核心模块http ...

  4. CS小分队第二阶段冲刺站立会议(5月29日)

    昨日成果:昨天在为主界面设计自主添加应用快捷方式功能,连续遇到困难. 遇到的困难:1.string字符串数组无法在单击事件中使用,提示string无法在eventargs中检索,尝试了各种方式都不行 ...

  5. 2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest Problem G. The Debut Album

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229669 时间限制:1s 空间限制:64MB 题目大意:给定n,a,b的值 求一个长度为n的 ...

  6. springboot+vue+element:echarts开发遇见问题---后端sql(三)

    <select id="getSumRequestRankingCount" parameterType="java.lang.String" resul ...

  7. JAVA方法的重载(overload)和覆盖(override)

    方法的重载(overload)和覆盖(override) 有的时候,类的同一种功能有多种实现方式,到底采用哪种实现方式,取决于调用者给定的参数.例如我们最常用的System.out.println() ...

  8. 简述Java中Http/Https请求监听方法

    一.工欲善其事必先利其器 做Web开发的人总免不了与Http/Https请求打交道,很多时候我们都希望能够直观的的看到我们发送的请求参数和服务器返回的响应信息,这个时候就需要借助于某些工具啦.本文将采 ...

  9. 测试——约跑APP

    项目名:约跑APP 用户需求规格说明书URL:http://www.cnblogs.com/liquan/p/6071804.html 组长博客URL:http://www.cnblogs.com/l ...

  10. CentOS 6.5安装配置LAMP服务器(Apache+PHP5+MySQL)

    1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 8 ...