hdu 4406 GPA 最大费用最大流

题意:给定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;
}
时间: 2024-10-14 06:21:59

hdu 4406 GPA 最大费用最大流的相关文章

HDU 4406 GPA(网络流-最大费用流)

GPA Problem Description GPA(Grade-Point Average) is one way to measure students' academic performance in PKU. Each course has an integer credit, ranges from 1 to 99. For each course, you will get a score at the end of the semester, which is an intege

HDU 2686 Matrix(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

HDU 4862 JUMP 最小费用最大流

2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定要访问到,并且每次访问的是没访问过的点,跳跃的方向为向右或者向下. 建图的时候,分成二分图,从一个超级源点向x部分建cap为1 cost为0的点,对所以可到达的点从x到y建cap为1,cost根据题目算出来,不过要算负值,因为我们要求得实际是最大费用,最后对结果求相反数即可.所有y部分的点对超级汇点

HDU 4406 GPA

GPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 440664-bit integer IO format: %I64d      Java class name: Main GPA(Grade-Point Average) is one way to measure students’ academic performance in PKU. Each co

hdu 4494 Teamwork 最小费用最大流

Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 Description Some locations in city A has been destroyed in the fierce battle. So the government decides to send some workers to repair these location

hdu 2686 Matrix 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.Every time yifenfei should to do is that choose a detour which frome the top left

HDU 3667Transportation(最小费用最大流)拆边,经典

Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2348 Accepted Submission(s): 1000 Problem Description There are N cities, and M directed roads connecting them. Now you want to trans

HDU 3488--Tour【最小费用最大流 &amp;&amp; 有向环最小权值覆盖 &amp;&amp; 经典】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2308    Accepted Submission(s): 1156 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 MCMF算法,我写的是MCMF算法,一开始想的是连10000个点,但是不会连那些大众点之间的边,只会连超级点和普通点之间的边.后来觉得只要连房子点和 人点就可以了.连从人到房子的边,容量是1,花费是他们之间的曼哈顿距离,然后超级源点和超级汇点像上面那样连接,注意连点的时候把他们每个点都具体化一下,就