[cogs461] [Network Flow 24 # #] Napkins [network flow, minimum cost maximum flow]

Source: Internet
Author: User

Build: From the source point to the first layer of the edge, the first layer of the day to use out how many napkins, the second layer of how many napkins are needed for the day, so pay attention to the purchase of the napkin edge capacity for infinity, to start from the source point to the second level point, every day there may be surplus, Nodes that are represented in the first layer as flowing into the second day. See the code, the first time to write the cost flow, do not know the template is right ...

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<ctime>#include<cstdlib>#include<queue>using namespacestd;template<Const int_n,Const int_m>structedge{structEdge_base {intTo,next,w,c;}    E[_M]; intCnt,p[_n];    Edge () {clear ();} intStartConst intx) {returnp[x];} voidInsertConst intXConst intYConst intZConst intZZ) {e[++cnt].to=y; E[CNT].NEXT=P[X]; E[cnt].w=z; E[cnt].c=zz; p[x]=cnt;return ; } voidClear () {cnt=1, Memset (P,0,sizeof(p)); } edge_base&operator[](Const intx) {returne[x];};intn,dis[110000],sss,ttt,cur[110000],a[110000];intCost,flow;BOOLvisited[110000]; Edge<110000,210000>e;BOOLSPFA (Const intS) {    inti,t,temp; Queue<int>Q; memset (Dis,0x3f,sizeof(Dis)); Dis[s]=0; Visited[s]=true;    Q.push (S);  while(!Q.empty ()) {T=q.front (), Q.pop (); visited[t]=false;  for(I=e.start (t); i;i=E[i].next) {Temp=e[i].to; if(E[I].W && dis[t]+e[i].c<Dis[temp]) {Dis[temp]=dis[t]+e[i].c; if(!Visited[temp]) {Visited[temp]=true;                Q.push (temp); }            }        }    }    returndis[ttt]!=0x3f3f3f3f;}intDfs (Const intSConst intBK) {    if(S==TTT)returnBK; Visited[s]=true; intrest=K;  for(int&i=cur[s];i;i=E[i].next) {        if(!visited[e[i].to] && dis[s]+e[i].c==dis[e[i].to] &&E[I].W) {            intflow=Dfs (E[i].to,min (REST,E[I].W)); Cost+=flow*e[i].c; E[I].W-=flow; E[i^1].w+=flow; if((Rest-=flow) <=0) Break; }    }    if(bk==rest) dis[s]=0x3f3f3f3f; Visited[s]=false; returnbk-Rest;} Pair<int,int>Dinic () { while(SPFA (SSS)) {memcpy (CUR,E.P,sizeof(cur)); Flow+=dfs (SSS,0x3f3f3f3f); }    returnMake_pair (flow,cost);}intMain () {Freopen ("napkin.in","R", stdin); Freopen ("Napkin.out","W", stdout); intI,NEWN,TS,CS,TF,CF; scanf ("%d",&N);  for(i=1; i<=n;++i) scanf ("%d",&A[i]); scanf ("%d%d%d%d%d",&newn,&ts,&cs,&tf,&CF); SSS=n+n+1; ttt=sss+1;  for(i=1; i<=n;++i) {E.insert (sss,i,a[i],0), E.insert (I,sss,0,0); E.insert (i+n,ttt,a[i],0), E.insert (Ttt,i+n,0,0); E.insert (Sss,i+n,0x3f3f3f3f, newn), E.insert (I+n,sss,0,-newn); if(i!=n) E.insert (i,i+1,0x3f3f3f3f,0), E.insert (i+1I0,0); if(i+tf<=n) E.insert (I,i+tf+n,0x3f3f3f3f, CF), E.insert (I+tf+n,i,0,-CF); if(i+ts<=n) E.insert (I,i+ts+n,0x3f3f3f3f, CS), E.insert (I+ts+n,i,0,-CS); } printf ("%d\n", Dinic (). second); return 0;}

[cogs461] [Network Flow 24 # #] Napkins [network flow, 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.