BZOJ 2424: [HAOI2010]訂貨 費用流

來源:互聯網
上載者:User

標籤:

2424: [HAOI2010]訂貨

Time Limit: 1 Sec  

Memory Limit: 256 MB

題目串連http://www.lydsy.com/JudgeOnline/problem.php?id=2424
Description

某公司估計市場在第i個月對某產品的需求量為Ui,已知在第i月該產品的訂貨單價為di,上個月月底未銷完的單位產品要付存貯費用m,假定第一月月初的庫存量為零,第n月月底的庫存量也為零,問如何安排這n個月訂購計劃,才能使成本最低?每月月初訂購,訂購後產品立即到貨,進庫並供應市場,於當月被售掉則不必付存貯費。假設倉庫容量為S。

Input第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)Output只有1行,一個整數,代表最低成本Sample Input

3 1 1000
2 4 8
1 2 4

Sample Output34HINT 

題意

 

題解:

費用流,S--月--T

                 |

      下個月

代碼:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int MAXN = 10000;const int MAXM = 100000;const int INF = 0x3f3f3f3f;struct Edge{    int to, next, cap, flow, cost;    int x, y;} edge[MAXM],HH[MAXN],MM[MAXN];int head[MAXN],tol;int pre[MAXN],dis[MAXN];bool vis[MAXN];int N, M;char map[MAXN][MAXN];void init(){    N = MAXN;    tol = 0;    memset(head, -1, sizeof(head));}void addedge(int u, int v, int cap, int cost)//左端點,右端點,容量,花費{    edge[tol]. to = v;    edge[tol]. cap = cap;    edge[tol]. cost = cost;    edge[tol]. flow = 0;    edge[tol]. next = head[u];    head[u] = tol++;    edge[tol]. to = u;    edge[tol]. cap = 0;    edge[tol]. cost = -cost;    edge[tol]. flow = 0;    edge[tol]. next = head[v];    head[v] = tol++;}bool spfa(int s, int t){    queue<int>q;    for(int i = 0; i < N; i++)    {        dis[i] = INF;        vis[i] = false;        pre[i] = -1;    }    dis[s] = 0;    vis[s] = true;    q.push(s);    while(!q.empty())    {        int u = q.front();        q.pop();        vis[u] = false;        for(int i = head[u]; i != -1; i = edge[i]. next)        {            int v = edge[i]. to;            if(edge[i]. cap > edge[i]. flow &&                    dis[v] > dis[u] + edge[i]. cost )            {                dis[v] = dis[u] + edge[i]. cost;                pre[v] = i;                if(!vis[v])                {                    vis[v] = true;                    q.push(v);                }            }        }    }    if(pre[t] == -1) return false;    else return true;}//返回的是最大流, cost存的是最小費用int minCostMaxflow(int s, int t, int &cost){    int flow = 0;    cost = 0;    while(spfa(s,t))    {        int Min = INF;        for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to])        {            if(Min > edge[i]. cap - edge[i]. flow)                Min = edge[i]. cap - edge[i]. flow;        }        for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to])        {            edge[i]. flow += Min;            edge[i^1]. flow -= Min;            cost += edge[i]. cost * Min;        }        flow += Min;    }    return flow;}int main(){    int n, m, ss;    scanf("%d%d%d",&n,&m,&ss);    int ch = 0, cm = 0;    init();//注意    int beg = 0;//超級起點    int end = 3000;//超級匯點    for(int i=1;i<=n;i++)    {        int x;scanf("%d",&x);        addedge(i,end,x,0);    }    for(int i=1;i<=n;i++)    {        int x;scanf("%d",&x);        addedge(0,i,9999999,x);    }    for(int i=1;i<n;i++)addedge(i,i+1,ss,m);    int ans = 0;    minCostMaxflow(beg,end,ans);    printf("%d\n",ans);}

 

BZOJ 2424: [HAOI2010]訂貨 費用流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.