[HAOI2010]订货 BZOJ2424

分析:

能看出来,这是一个费用流的题,建图很朴实,i连i+1,费用为存储费用,流量为仓库容量,之后S连i,费用为单价,流量为inf,之后i连T,流量为a[i],费用为0,之后裸上费用流...

附上代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
#define N 55
#define S 0
#define T 51
struct node
{
    int to,next,val,flow,from;
}e[N*20];
int head[N],cnt,dis[N],vis[N],from[N],ans,n,m,s;
void add(int x,int y,int z,int v)
{
    e[cnt].to=y,e[cnt].next=head[x],e[cnt].val=v;
    e[cnt].flow=z,e[cnt].from=x,head[x]=cnt++;
}
void insert(int x,int y,int z,int v)
{
    add(x,y,z,v);add(y,x,0,-v);
}
int spfa()
{
    memset(dis,0x3f,sizeof(dis));memset(from,-1,sizeof(from));
    queue <int>q;q.push(S);dis[S]=0;
    while(!q.empty())
    {
        int x=q.front();q.pop();vis[x]=0;
        for(int i=head[x];i!=-1;i=e[i].next)
        {
            int to1=e[i].to;
            if(e[i].flow&&dis[to1]>dis[x]+e[i].val)
            {
                from[to1]=i;
                dis[to1]=dis[x]+e[i].val;
                if(!vis[to1])vis[to1]=1,q.push(to1);
            }
        }
    }
    return dis[T]==0x3f3f3f3f?0:1;
}
void mcf()
{
    int x,i=from[T];
    while(i!=-1){x=min(e[i].flow,x);i=from[e[i].from];}
    i=from[T];
    while(i!=-1){e[i].flow-=x,e[i^1].flow+=x,ans+=x*e[i].val,i=from[e[i].from];}
    return ;
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1;i<n;i++)insert(i,i+1,s,m);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        insert(i,T,x,0);
    }
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        insert(S,i,20000,x);
    }
    while(spfa())mcf();
    printf("%d\n",ans);
    return 0;
}

  

posted @ 2018-05-31 20:41  Winniechen  阅读(154)  评论(0编辑  收藏  举报