现在的位置: 首页 > 综合 > 正文

hdu 4406 GPA 最大费用最大流

2019年09月03日 ⁄ 综合 ⁄ 共 3266字 ⁄ 字号 评论关闭

题意:给定n,k,m分别代表天数,每天上的课,以及科目数。

            给定每门课的学分,已经基础分数。

            给定n天每天有哪些课能学。

            求如何安排复习,使得GPA尽可能大且没有挂科,算出GPA。

思路:最大费用最大流。定义函数f(score,credit)=credit×(4-3.0/1600×(100-score)^2)。每一天向汇点连边,容量为k,费用0;  每门课

与每天根据是否在当天能上连边,容量为k,费用为0;
源点向每门课连边,若基础分score小于60,则与源点连一条流量60-score、费用

inf的边,保证优先到达60,接着从源点与该课程连40条流量分别为1,费用为f(x+1,)-f(x,p)的边(40条代表61到100);若基础分score

大于等于60,则连100-score条容量1、费用f(x+1,p)-f(x,p)的边。跑完最大费用最大流之后,再看是否还有哪门课不及格,如果有ans=0,不然计算GPA。详见代码:

/*********************************************************
  file name: hdu4406.cpp
  author : kereo
  create time:  2015年02月11日 星期三 15时35分08秒
*********************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int sigma_size=26;
const int N=100+50;
const int MAXN=100000+50;
const int inf=0x3fffffff;
const double eps=1e-8;
const int mod=1000000000+7;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define PII pair<int, int>
#define mk(x,y) make_pair((x),(y))
int n,m,k,edge_cnt;
double d[N];
int head[N],inq[N],pre[N],A[N],w[N],score[N];
struct Edge{
    double cost;
    int v,cap,flow,next;
}edge[MAXN];
void init(){
    edge_cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,double cost){
    edge[edge_cnt].v=v; edge[edge_cnt].cap=cap; edge[edge_cnt].flow=0; 
    edge[edge_cnt].cost=cost; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++;
    edge[edge_cnt].v=u; edge[edge_cnt].cap=0; edge[edge_cnt].flow=0;
    edge[edge_cnt].cost=-cost; edge[edge_cnt].next=head[v]; head[v]=edge_cnt++;
}
bool spfa(int st,int ed,int &flow,double &cost){
    memset(inq,0,sizeof(inq));
    for(int i=0;i<=ed;i++) d[i]=-1.0*inf;
    d[st]=0; inq[st]=1; pre[st]=st; A[st]=inf;
    queue<int>Q;
    Q.push(st);
    while(!Q.empty()){
        int u=Q.front(); Q.pop();
        inq[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            if(edge[i].cap>edge[i].flow && d[v]<d[u]+edge[i].cost){
                d[v]=d[u]+edge[i].cost;
                A[v]=min(A[u],edge[i].cap-edge[i].flow);
                pre[v]=i;
                if(!inq[v]){
                    inq[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(fabs(d[ed]+inf)<eps)
        return false;
    flow+=A[ed]; cost+=A[ed]*d[ed];
    int u=ed;
    while(u!=st){
        edge[pre[u]].flow+=A[ed];
        edge[pre[u]^1].flow-=A[ed];
        u=edge[pre[u]^1].v;
    }
    return true;
}
double MaxCostFlow(int st,int ed){
    int flow=0;
    double cost=0;
    while(spfa(st,ed,flow,cost)) ;
    return flow;
}
double func(int x,int w){
    return (4.0-3.0*(100-x)*(100-x)/1600)*w;
}
int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&k,&m) && n+k+m){
        init();
        for(int i=1;i<=m;i++)
            scanf("%d",&w[i]);
        for(int i=1;i<=m;i++)
            scanf("%d",&score[i]);
        for(int i=1;i<=n;i++)
            addedge(i+m,n+m+1,k,0);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                int x;
                scanf("%d",&x);
                if(x)
                    addedge(j,i+m,k,0);
            }
        }
        double res;
        for(int i=1;i<=m;i++){
            if(score[i]<60){
                addedge(0,i,60-score[i],inf*1.0);
                res=func(60,w[i]);
                for(int j=61;j<=100;j++){
                    double tmp=func(j,w[i]);
                    addedge(0,i,1,tmp-res);
                    res=tmp;
                }
            }
            else{
                res=func(score[i],w[i]);
                for(int j=score[i]+1;j<=100;j++){
                    double tmp=func(j,w[i]);
                    addedge(0,i,1,tmp-res);
                    res=tmp;
                }
            }
        }
        MaxCostFlow(0,n+m+1);
        for(int i=head[0];i!=-1;i=edge[i].next)
            score[edge[i].v]-=edge[i^1].flow;
        /*for(int i=1;i<=m;i++){
            printf(" %d",score[i]);
        }
        printf("\n");*/
        int sum=0;
        for(int i=1;i<=m;i++)
            sum+=w[i];
        double ans=0.0;
        int flag=1;
        for(int i=1;i<=m;i++){
            if(score[i]<60){
                flag=0;
                break;
            }
            ans+=func(score[i],w[i])/sum;
        }
        //printf("flag=%d\n",flag);
        if(!flag)
            ans=0;
        printf("%.6f\n",ans);
    }
	return 0;
}

抱歉!评论已关闭.