CodeChef - RIN Course Selection

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 jX[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: NM 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 ≤ MN ≤ 100
  • 0 ≤ K ≤ 100
  • -1 ≤ X[i][j] ≤ 100
  • 1 ≤ A[i]B[i] ≤ N
  • For each iA[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;
}

  

posted @ 2018-04-20 20:37  蒟蒻JHY  阅读(245)  评论(0编辑  收藏  举报