[HAOI2010]订货

题目描述

某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初的库存量为零,第n月月底的库存量也为零,问如何安排这n个月订购计划,才能使成本最低?每月月初订购,订购后产品立即到货,进库并供应市场,于当月被售掉则不必付存贮费。假设仓库容量为S。

输入输出格式

输入格式:
第1行:n, m, S (0<=n<=50, 0<=m<=10, 0<=S<=10000)

第2行:U1 , U2 , … , Ui , … , Un (0<=Ui<=10000)

第3行:d1 , d2 , …, di , … , dn (0<=di<=100)

输出格式:
只有1行,一个整数,代表最低成本

输入输出样例

输入样例#1:
3 1 1000
2 4 8
1 2 4

输出样例#1:
34

.
.
.
.
.
分析
这是一道费用流裸题

.
.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<queue>
using namespace std;

int n,m,S,cnt,last[55],s,t,dis[55],vis[55],pre[55],ans=0; 
struct edge
{
	int from,to,c,w,next;
}e[55*55*2]; 

queue<int> q;

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void addedge(int u,int v,int c,int w) 
{
    e[++cnt].from=u;e[cnt].to=v;e[cnt].c=c;e[cnt].w=w;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].from=v;e[cnt].to=u;e[cnt].c=0;e[cnt].w=-w;e[cnt].next=last[v];last[v]=cnt;
    
}

bool spfa() 
{
    for (int i=s;i<=t;i++) 
		dis[i]=2147483647; 	
    dis[s]=0;
	vis[s]=1; 
	q.push(s);
    while (!q.empty())
    {
        int u=q.front();
		q.pop();
        for (int i=last[u];i;i=e[i].next)
            if (e[i].c&&(dis[u]+e[i].w<dis[e[i].to])) 
            {
                dis[e[i].to]=dis[u]+e[i].w; 
                pre[e[i].to]=i; 
                if (!vis[e[i].to]) 
                {
                	vis[e[i].to]=1;
					q.push(e[i].to); 
                }
               
            }
        vis[u]=0;
    }
    if (dis[t]<2147483647) return true; else return false;
}

void mcf()
{
    int mn=2147483647,x=t;
   
    while (pre[x]) 
    {
        mn=min(mn,e[pre[x]].c); 
        x=e[pre[x]].from; 
    }
    ans+=dis[t]*mn;
	x=t; 
    while (pre[x]) 
    {
        e[pre[x]].c-=mn; 
        e[pre[x]^1].c+=mn; 
        x=e[pre[x]].from; 
    }
}

int main()
{
    n=read();m=read();S=read();
    s=0;t=n+1;cnt=1;
    for (int i=1;i<=n;i++)
    {
        int x=read();
        addedge(i,t,x,0); 
    }
    for (int i=1;i<=n;i++)
    {
        int x=read();
        addedge(s,i,2147483647,x); 
    }
    for (int i=1;i<n;i++) 
		addedge(i,i+1,S,m); 
    while (spfa()) mcf(); 
    printf("%d",ans); 
    return 0;
}

转载于:https://www.cnblogs.com/YYC-0304/p/11094942.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值