bzoj2424: [HAOI2010]订货
最小费用最大流即可。-=a[t] 写成了-=-a[t],于是为此找了很久的错QAQsading
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define clr(x,c) memset(x,c,sizeof(x)) #define REP(i,s,t) for(int i=s;i<=t;i++) #define qwq(x) for(edge*e=head[x];e;e=e->next) const int nmax=55; const int inf=0x3f3f3f3f; int x;char c; int read(){ x=0;c=getchar();bool f=true; while(!isdigit(c)){ if(c=='-') f=false; c=getchar(); } while(isdigit(c)) x=x*10+c-'0',c=getchar(); return f?x:-x; } struct edge{ int to,cap,cost;edge *next,*rev; }; edge edges[nmax<<3],*pt,*head[nmax],*p[nmax]; void add(int u,int v,int d,int w){ pt->to=v;pt->cap=d;pt->cost=w;pt->next=head[u];head[u]=pt++; } void adde(int u,int v,int d,int w){ add(u,v,d,w);add(v,u,0,-w);head[u]->rev=head[v];head[v]->rev=head[u]; } int d[nmax],a[nmax];bool inq[nmax]; int mincost(int s,int t){ int flow=0,cost=0; while(1){ clr(inq,0);clr(d,0x3f);d[s]=0;inq[s]=1;a[s]=inf; queue<int>q;q.push(s); while(!q.empty()){ int x=q.front();q.pop();inq[x]=0; qwq(x) if(e->cap>0&&d[e->to]>d[x]+e->cost){ int to=e->to; d[to]=d[x]+e->cost; a[to]=min(a[x],e->cap);p[to]=e; if(!inq[to]) q.push(to),inq[to]=1; } } if(d[t]==inf) break; flow+=a[t];cost+=d[t]*a[t]; int x=t; while(x!=s) p[x]->cap-=a[t],p[x]->rev->cap+=a[t],x=p[x]->rev->to; // printf("%d %d %d\n",d[t],flow,cost); } return cost; } int main(){ int n=read(),m=read(),v=read(); int s=0,t=n+1,tmp; pt=edges;clr(head,0); rep(i,n) tmp=read(),adde(i,t,tmp,0); rep(i,n) tmp=read(),adde(s,i,inf,tmp); rep(i,n-1) adde(i,i+1,v,m); /*REP(i,0,n+1){ qwq(i) printf("%d %d %d ",e->to,e->cap,e->cost); printf("\n"); }*/ printf("%d\n",mincost(s,t)); return 0; }
2424: [HAOI2010]订货
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 809 Solved: 564
[Submit][Status][Discuss]
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
2 4 8
1 2 4
Sample Output
34