CGOS461 [Network Flow 24] Napkin (minimum cost maximum flow)

Source: Internet
Author: User

The title says:

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. The use of napkins 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. Take the napkin to the slow wash, wash a piece to be 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.

Very interesting question, at least also need to think a little bit. Consider the maximum flow with minimum cost.

    • First of all, it is clear that each day as a point to the meeting point of the capacity of the day to the number of napkins and the cost of 0 of the side, so that the maximum flow to meet the requirements of the supply of the conditions of the day;
    • Then for the purchase of napkins, the source point to each day of the capacity of the INF cost P edge;
    • And finally, we need to build a napkin to reuse the side of the napkin, so consider:
      • For the day I will have ri a napkin can be reused, and the J-day (j>=i+m) can get fast washing the first day of the napkin, unit cost is F, slow wash the same;
      • This is clear: again new n vertices, the source point to the first new point of the capacity RI cost 0 edge, the first new point to the point of J Day (J>=i+m) The edge of the capacity INF cost F, the first I new point to the point of K days (K>=i+n) The edge of the capacity INF cost S.

In this way, the minimum cost of the chart run is the maximum flow OK.

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineINF (1&LT;&LT;30)7 #defineMAXN 4448 #defineMAXM 444*8889 structedge{Ten     intU,v,cap,cost,next; One }EDGE[MAXM]; A intHEAD[MAXN]; - intNV,NE,VS,VT; -  the voidAddedge (intUintVintCapintCost ) { -Edge[ne].u=u; Edge[ne].v=v; Edge[ne].cap=cap; edge[ne].cost=Cost ; -Edge[ne].next=head[u]; head[u]=ne++; -Edge[ne].u=v; Edge[ne].v=u; edge[ne].cap=0; edge[ne].cost=-Cost ; +EDGE[NE].NEXT=HEAD[V]; head[v]=ne++; - } + BOOLVIS[MAXN]; A intD[MAXN],PRE[MAXN]; at BOOLSPFA () { -      for(intI=0; i<nv;++i) { -vis[i]=0; -d[i]=INF; -     } -vis[vs]=1; ind[vs]=0; -queue<int>que; to Que.push (VS); +      while(!Que.empty ()) { -         intu=Que.front (); Que.pop (); the          for(intI=head[u]; i!=-1; I=Edge[i].next) { *             intv=edge[i].v; $             if(Edge[i].cap && d[v]>d[u]+edge[i].cost) {Panax Notoginsengd[v]=d[u]+Edge[i].cost; -pre[v]=i; the                 if(!Vis[v]) { +vis[v]=1; A Que.push (v); the                 } +             } -         } $vis[u]=0; $     } -     returnd[vt]!=INF; - } the intMCMF () { -     intres=0;Wuyi      while(SPFA ()) { the         intflow=inf,cost=0; -          for(intU=VT; U!=vs; u=edge[pre[u]].u) { Wuflow=min (flow,edge[pre[u]].cap); -         } About          for(intU=VT; U!=vs; u=edge[pre[u]].u) { $edge[pre[u]].cap-=flow; -edge[pre[u]^1].cap+=flow; -cost+=flow*Edge[pre[u]].cost; -         } Ares+=Cost ; +     } the     returnRes; - } $ intneed[222]; the intMain () { theFreopen ("napkin.in","R", stdin); theFreopen ("Napkin.out","W", stdout); the     intn,p,a,b,x,y; -scanf"%d",&n); in      for(intI=1; i<=n; ++i) { thescanf"%d", need+i); the     } Aboutscanf"%d%d%d%d%d",&p,&a,&b,&x,&y); thevs=0; vt=n*2+1; nv=vt+1; Ne=0; thememset (head,-1,sizeof(head)); the      for(intI=1; i<=n; ++i) { +Addedge (vs,i+n,inf,p); -Addedge (I+n,vt,need[i],0); theAddedge (Vs,i,need[i],0);Bayi          for(intJ=i+a; j<=n; ++J) Addedge (i,j+n,inf,b); the          for(intJ=i+x; j<=n; ++J) Addedge (i,j+n,inf,y); the     } -printf"%d", MCMF ()); -     return 0; the}

CGOS461 [Network Flow 24] Napkin (minimum cost maximum flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.