"Cost Flow" bzoj1221-[hnoi2001) software development

Source: Internet
Author: User

"The main topic"

A software company is planning an n-day software development program, according to the development plan I need NI software developers, in order to improve the efficiency of software developers, the company provides software personnel with a lot of services, one of the services is to provide each developer a daily disinfection towel, This disinfectant towel must be sterilized after one day to be used. Disinfection methods There are two, a mode of disinfection needs a day, B mode of disinfection needs B Day (B>a), a disinfection method of the cost of each towel FA, B disinfection method of the cost of each towel fb, and buy a new towel for the cost of f (new towel is disinfected, the day can be used); >FA>FB. The company's manager is planning how many new towels to buy each day, how many towels per day, and how many towels are sent per day for the B disinfection of these n days. Of course, company managers want the lowest cost. Your job is to plan how many towels to buy per day, how many towels per day, and how many towels to disinfect for B, so that the company has the lowest total cost of providing towel service in this N-day software development.

Ideas

Each day is split into two points, the left side of the towel will be disinfected, the right side of the towel is good disinfection, to build the map.

Set a source point s and meeting point T

• An edge with a capacity of towel[i] at a cost of 0 per day from the source point, and an edge with a capacity of towel[i per day to the meeting point, and a cost of 0.

Since each towel can be disinfected with the AB method
• A capacity of +ta+1 is provided by the I-to (i+n), the cost is the side of the FA, and by the I-to (i+n) +tb+1 a capacity of INF, the cost for the side of the FB

Because the unused towel of the day can be left directly to the next day
• An edge with a capacity of INF and a cost of f from the source point to the I+n

As the towel of the day can be left until the next day for cleaning
• An edge with a capacity of INF of 0 for the I-to-i+1

Because the mid-edge capacity is INF, the maximum flow is guaranteed to be full-flow, in which case the minimum cost flow answer is minimum cost.

"Error Point"

Made a little sporadic mistake, still is the initialization error + hand residual hit wrong, has been written into the comments.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6#include <queue>7 #defineS 08 #defineT 2*n+19 using namespacestd;Ten Const intmaxn= -+ -; One Const intinf=0x7fffffff; A structnode - { -     intTo,pos,cap,cost; the }; -  -Vector<node>E[MAXN]; - intN,TA,TB,F,FA,FB; + intTOWEL[MAXN]; - intPRE[MAXN],PREEDGE[MAXN]; +  A voidAddedge (intUintVintWintc) at { - e[u].push_back (node) {v,e[v].size (), w,c}); -E[v].push_back (node) {u,e[u].size ()-1,0,-C});  - } -  - voidInit () in { -scanf"%d%d%d%d%d%d",&n,&ta,&tb,&f,&fa,&FB); to       for(intI=1; i<=n;i++) +      { -scanf"%d",&towel[i]); the           *          /*split a day into two points, $ side with a capacity of towel[i] at a cost of 0 per day from the source pointPanax Notoginseng a side with a capacity of towel[i] and a cost of 0 per day to the meeting point*/ -Addedge (S,i,towel[i],0); theAddedge (I+n,t,towel[i],0); +           A          /*since each towel can be disinfected with the AB method the by I to (I+n) +ta+1 a capacity for INF, the cost for the side of the FA + by I to (I+n) +tb+1 a capacity for INF, the cost for the side of the FB*/ -         if(i+n+ta+1<=2*n) Addedge (i,i+n+ta+1, INF,FA); $         if(i+n+tb+1<=2*n) Addedge (i,i+n+tb+1, INF,FB); $  -         /*because the unused towel of the day can be left directly to the next day - or the towel of the day can be left until the next day for cleaning. the an edge with a capacity of INF and a cost of f from the source point to the I+n - from I to i+1 a capacity of INF, the cost of 0 side*/WuyiAddedge (s,i+n,inf,f); the         if(i+1<=n) Addedge (i,i+1Inf0); -     } Wu } -  About intSPFA () $ { -     intVIS[MAXN],DIS[MAXN]; -queue<int>que; -memset (Vis,0,sizeof(Vis)); Amemset (pre,-1,sizeof(pre)); +      for(intI=1; i<=t;i++) Dis[i]=inf;//This starts with a hasty initialization of 127, in fact the value is not equal to the INF, which results in an error in judgment . thevis[s]=1; -dis[s]=0; $ Que.push (S); the      while(!que.empty ()) the     { the         intHead=Que.front (); Que.pop (); thevis[head]=0; -          for(intI=0; I<e[head].size (); i++)//here E[i].size () looked for a half-day to find Orz in         { theNode &tmp=E[head][i]; the             if(tmp.cap>0&& dis[tmp.to]>dis[head]+tmp.cost) About             { thedis[tmp.to]=dis[head]+Tmp.cost; thepre[tmp.to]=head; thepreedge[tmp.to]=i; +                 if(!vis[tmp.to]) -                 { thevis[tmp.to]=1;Bayi Que.push (tmp.to); the                 } the             } -         } -     } the     if(Dis[t]==inf)return 0;Else return 1; the } the  the voidMCMF () - { the     intans=0; the      while(SPFA ()) the     {94         intflow=INF; the          for(inti=t;pre[i]!=-1; i=Pre[i]) the         { theflow=min (flow,e[pre[i]][preedge[i]].cap);98         } About          for(inti=t;pre[i]!=-1; i=Pre[i]) -         {101Node &tmp=E[pre[i]][preedge[i]];102ans+=flow*Tmp.cost;103tmp.cap-=flow;104e[tmp.to][tmp.pos].cap+=flow; the         }106     }107cout<<ans<<Endl;108 }109  the intMain ()111 { the init ();113 MCMF (); the     return 0; the}

"Cost Flow" bzoj1221-[hnoi2001) software development

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.