zoukankan      html  css  js  c++  java
  • [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;
    }
    
  • 相关阅读:
    Delphi 的RTTI机制浅探3(超长,很不错)
    关于跨进程使用回调函数的研究:以跨进程获取Richedit中RTF流为例(在Delphi 初始化每一个TWinControl 对象时,将会在窗体 的属性(PropData)中加入一些标志,DLL的HInstance的值与HOST 进程的HInstance并不一致)
    获得QQ聊天输入框中的内容
    使用Jenkins来构建Docker容器
    各种排序算法汇总
    ASP.NET Web API和ASP.NET Web MVC中使用Ninject
    s性能优化方面的小知识
    算法时间复杂度的计算
    js模块开发
    NET Framework 4.5 五个新特性
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/11094942.html
Copyright © 2011-2022 走看看