bzoj2424: [HAOI2010]订货

MDZZ。。这个做题状态就是要GG的节奏啊。。SPFA码错也是醉了。。

费用流模版题,每天和st(inf,di),ed(ui,0)连,天与天之间连(S,m)

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int inf=2147483647;
struct node
{
    int x,y,c,d,next,other;
}a[510000];int len,last[11000];
void ins(int x,int y,int c,int dd)
{
    int k1,k2;
    len++;k1=len;
    a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=dd;
    a[len].next=last[x];last[x]=len;
     
    len++;k2=len;
    a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-dd;
    a[len].next=last[y];last[y]=len;
     
    a[k1].other=k2;
    a[k2].other=k1;
}
int n,st,ed,ans;
int d[11000],list[11000];
bool v[11000];
int pre[11000],cc[11000];
bool spfa()
{
    memset(cc,0,sizeof(cc));cc[st]=inf;
    
    memset(v,false,sizeof(v));v[st]=true;
    memset(d,63,sizeof(d));d[st]=0;
    int head=1,tail=2;list[1]=st;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(d[y]>d[x]+a[k].d&&a[k].c>0)
            {
                d[y]=d[x]+a[k].d;
                if(v[y]==false)
                {
                    v[y]=true;
                    list[tail]=y;
                    tail++;if(tail==10500)tail=1;
                }
                cc[y]=min(a[k].c,cc[x]);
                pre[y]=k;
            }
        }
        v[x]=false;
        head++;if(head==10500)head=1;
    }
    
    if(d[ed]>=999999999)return false;
    int x=ed;
    while(x!=st)
    {
        int k=pre[x];
        a[k].c-=cc[ed];a[a[k].other].c+=cc[ed];
        ans+=a[k].d*cc[ed];
        x=a[k].x;
    }
    return true;
}
int main()
{
    int m,S,x;
    scanf("%d%d%d",&n,&m,&S);
    st=n+1;ed=n+2;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        ins(i,ed,x,0);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        ins(st,i,inf,x);
    }
    for(int i=1;i<n;i++)ins(i,i+1,S,m);
    
    ans=0;
    while(spfa()==true);
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2017-10-10 10:31  AKCqhzdy  阅读(128)  评论(0编辑  收藏  举报