bzoj2424[HAOI2010]订货
传送门
最小费用最大流,这个就真的是水题了,我看错题了,建了一个比正解复杂好几倍的图。。。我以为每个月有多个产品,还以为仓库可以无限时的存产品。。。然后建出来的图就巨复杂。。。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define min(a,b) (a<b?a:b)
#define rg register
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
int ss,tt,tot,sum,n,m,k,id[200001],pre[200001],nxt[200001],h[100001],sla[100001],s,t,v[200001],dis[100001],w[200001],cnt=1,inf=1e9,ans,f[200001];bool vis[100001];
void add(int x,int y,int z,int u)
{
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,v[cnt]=z,w[cnt]=u;
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt,v[cnt]=0,w[cnt]=-u;
}
int dfs(int x,int flow)
{
if(x==t){ans+=dis[t]*flow,sum+=flow;return flow;}
int f=flow;vis[x]=1;
for(rg int i=h[x];i;i=nxt[i])
if(!vis[pre[i]]&&v[i])
{
if(!(dis[x]+w[i]-dis[pre[i]]))
{
int y=dfs(pre[i],min(v[i],f));
f-=y,v[i]-=y,v[i^1]+=y;
if(!f)return flow;
}
else sla[pre[i]]=min(sla[pre[i]],dis[x]+w[i]-dis[pre[i]]);
}
return flow-f;
}
bool aug(int s,int t)
{
int mn=inf;
for(rg int i=s;i<=t;i++)if(!vis[i])mn=min(mn,sla[i]),sla[i]=inf;
if(mn==inf)return 1;
for(rg int i=s;i<=t;i++)if(!vis[i])dis[i]+=mn;
return 0;
}
void zkw(int s,int t)
{
memset(sla,63,sizeof sla);
while(1)
{
while(1){memset(vis,0,sizeof vis);if(!dfs(s,inf))break;}
if(aug(s,t))break;
}
}
int main()
{
read(n),read(m),read(k),s=0,t=n+1;
for(rg int i=1,x;i<=n;i++)read(x),add(i,t,x,0);
for(rg int i=1,x;i<=n;i++)read(x),add(s,i,inf,x);
for(rg int i=1;i<n;i++)add(i,i+1,k,m);
zkw(s,t),printf("%d\n",ans);
}