[Network Stream 24 questions] Napkins
"Problem description"
A restaurant in successive N days, the first day I need RI block napkins (i=l,2,...,n). The restaurant can get napkins from three different ways.
(1) Buy a new napkin, each piece needs p points;
(2) Send the used napkin to the quick wash, wash a piece of M days, the cost of f (f<p). such as M=l, the first day to the fast-washing department of the napkin can be used the next day, send slow washing is also the case.
(3) The napkin is sent to the slow wash, wash a piece of n Days (n>m), the cost of S (s<f).
At the end of each day, the restaurant must decide how many pieces of used napkins are delivered to the fast-washing section, and how many blocks are delivered to the slow wash. At the beginning of each day, the restaurant must decide whether to buy new napkins and how much, so that the washing and the new purchase of napkins and meet the demand for the day RI, and the total cost of n days is the smallest.
Input
The input file total 3 lines, the 1th Act total number of days, the 2nd act the number of napkin blocks required per day; 3rd Act the new purchase fee for each napkin: p, fast wash the required number of days m, fast washing required f, slow washing required days n, slow washing cost s.
Output
One line, the minimum cost
"Examples"
Napkin.in
3
3 2 4
10 1 6) 2 3
Napkin.out
64
"Data Size"
N<=200,ri<=50
This problem is very ingenious in modeling.
Just show the two-class decision.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 using namespacestd;6 Const intmaxn=510;7 Const intmaxm=8010;8 Const intinf=100000000;9 intCnt=1, FIR[MAXN],NXT[MAXM];Ten intTO[MAXM],CAP[MAXM],VAL[MAXM]; One voidAddedge (intAintBintCintv) { Anxt[++cnt]=fir[a];to[cnt]=b; -cap[cnt]=c;val[cnt]=v;fir[a]=CNT; - } the -queue<int>Q; - intPATH[MAXN]; - intDIS[MAXN],VIS[MAXN]; + intSPFA (intSintT) { - for(inti=s+1; i<=t;i++) +dis[i]=INF; AQ.push (S); vis[s]=1; at while(!Q.empty ()) { - intx=Q.front (); -Q.pop (); vis[x]=0; - for(intI=fir[x];i;i=Nxt[i]) - if(cap[i]&&dis[to[i]]>dis[x]+Val[i]) { -dis[to[i]]=dis[x]+Val[i]; in if(!Vis[to[i]] Q.push (To[i]); -vis[to[i]]=1;p ath[to[i]]=i; to } + } - returnDis[t]==inf?0:d is[t]; the } * $ int(intSintT) {Panax Notoginseng intf=inf,p=T; - while(p!=R) { thef=min (f,cap[path[p]]); +p=to[path[p]^1]; A } thep=T; + while(p!=S) { -cap[path[p]]-=F; $cap[path[p]^1]+=F; $p=to[path[p]^1]; - } - returnF; the } - Wuyi intMCMF (intSintT) { the intret=0, D; - while(d=SPFA (s,t)) WuRet+=aug (s,t) *D; - returnret; About } $ - intneed[maxn],s,t; - intN,B,F,FC,S,SC; - intMain () { AFreopen ("napkin.in","R", stdin); +Freopen ("Napkin.out","W", stdout); thescanf"%d", &n); s=0; t=n*2+1; - for(intI=1; i<=n;i++) $scanf"%d",&need[i]); thescanf"%d%d%d%d%d",&b,&f,&fc,&s,&SC); the the for(intI=1; i<=n;i++){ theAddedge (S,i,need[i],0); -Addedge (I,s,0,0); inAddedge (s,i+n,inf,b); theAddedge (I+n,s,0,-b); theAddedge (I+n,t,need[i],0); AboutAddedge (T,i+n,0,0); the if(i+f<=N) { theAddedge (i,i+f+N,INF,FC); theAddedge (I+f+n,i,0,-FC); + } - if(i+s<=N) { theAddedge (i,i+s+N,INF,SC);BayiAddedge (I+s+n,i,0,-SC); the } the if(i!=N) { -Addedge (i,i+1Inf0); -Addedge (i+1I0,0); the } the } theprintf"%d\n", MCMF (s,t)); the return 0; -}
Network flow (expense flow): [Network flow 24 questions] Napkins