uva11613 - Acme Corporation 费用流

Wile E. Coyote is back. He is back inthe business. The business of capturing the road runner. Being the most loyalcustomer to the Acme Corporation, they are hoping to do some great businesswith him. Although, Acme makes literally every kinds of devices, all of themhas a similarity, that has been kept secret for ages. All of their products usea secret element “element X” (this is kept so secret that, only you and theAcme people know about this). The decision makers of the Acme Corp. has alreadyestimated the maximum amount of element X that can be used into manufactureevery month.

 

For month i, the per unitmanufacturing cost of  “element X” is mi,and at most ni units can be produced. Moreover, theselling price for “element X” for that month is pi.One more thing is that, element X older than Ei monthscan not be used. But good thing is that, they can store any amount of element Xin their inventory (it's the Acme Corp, they can make anything :) ). Now, AcmeCorporation wants to know how much element X should be produced and sold, tomake the highest amount of profit.

 

 

 

 

Input

 

l       Firstline contains T, the number of test cases.

l       Foreach test case

u     Firstline contains two integers M and I, the number ofmonths to consider and the cost of storing per unit of element X per month inthe inventory.

u     Eachof the next M lines describe the parameters for each month

·         Theith line contains 5 integers, mi, ni,pi, si, Ei, where miis the per unit manufacturing cost for month i, niis the maximum amount that can be manufactured in this month, piis the selling price for that month(per unit), si isthe maximum amount that can be sold that month, and Eiis the maximum time,element X manufactured on month i, can bestored in the inventory. For example, if for month 1, E1 = 3, theelements produced in month 1 can be sold in months 1, 2, 3 and 4. But it cannot be sold in month 5.

 

 

Output

 

For each test case, output the casenumber and the maximum amount of profit, Acme Corporation can make. Note that,you have to think of only M months. If any amount of element X isstored in the inventory after this period, are completely ignored. Forformatting, see the sample input and output.

 

Constraints

 

l       

l       

l       

l       

 

 

 

Sample Input                                                                               Outputfor Sample Input

1

2 2

2 10 3 20 2

10 100 7 5 2

Case 1: 2

  生产一种东西,给出这种东西在未来M个月中每个月的单位售价、最大产量、生产成本、最大销售量、最大存储时间,每单位物品多存储一个月就要多花I元。求最大利润。

  给每个月建立两个点 i和i'。源点S向i连边,流量表示第i个月的产量,费用是生产成本。i'向T连边,容量为第i个月的销量,费用是负的销售价格。每个点i向i',(i+1)',(i+2)'...(i+Ei)'连边,容量是INF,如果是第i+j个月费用就是j*I。为什么一个月要用两个点呢?因为如果只用一个点的话,假设i保存1个月,i->i+1连边,如果i+1再和i+2连边的话就不符合题意了。

  这是不固定流量的最小费用流,因为每次都是找最短路增加流量的,所以每次都是当前流量下最小费用,这里费用负的最小就是利润最大,增广到费用大于等于0就不用再增广了。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXN 210
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
int T,M,I;
struct Edge{
    int from,to,cap,flow,cost;
};
struct MCMF{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[MAXN];
    int inq[MAXN];  //是否在队列中
    int d[MAXN];    //bellman
    int p[MAXN];    //上一条弧
    int a[MAXN];    //增广量

    void init(int n){
        this->n=n;
        for(int i=0;i<n;i++) G[i].clear();
        edges.clear();
    }
    void add_edge(int from,int to,int cap,int cost){
        edges.push_back((Edge){from,to,cap,0,cost});
        edges.push_back((Edge){to,from,0,0,-cost});
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool bellman(int s,int t,int& flow,LL& cost){
        for(int i=0;i<n;i++) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0;
        inq[s]=1;
        p[s]=0;
        a[s]=INF;
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            inq[u]=0;
            int len=G[u].size();
            for(int i=0;i<len;i++){
                Edge& e=edges[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){
                        q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }
        if(d[t]==INF) return false;
        if(d[t]>0) return false;
        flow+=a[t];
        cost+=(LL)d[t]*a[t];
        int u=t;
        while(u!=s){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
            u=edges[p[u]].from;
        }
        return true;
    }
    LL mincost(int s,int t){
        int flow=0;
        LL cost=0;
        while(bellman(s,t,flow,cost));
        return cost;
    }
}g;

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&M,&I);
        int s=0,t=2*M+1;
        g.init(2*M+2);
        int m,n,p,q,e;
        for(int i=1;i<=M;i++){
            scanf("%d%d%d%d%d",&m,&n,&p,&q,&e);
            g.add_edge(s,i,n,m);
            for(int j=0;j<=e;j++) if(i+j<=M) g.add_edge(i,i+j+M,INF,I*j);
            g.add_edge(i+M,t,q,-p);
        }
        printf("Case %d: %lld\n",++cas,-g.mincost(s,t));
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值