poj2135 Minimum cost flow

Source: Internet
Author: User

For the minimum cost flow, the basic idea and the maximum flow are similar, constantly looking for augmented road augmentation, but also consider the cost problem at this time.

The method of finding the maximum flow is to find a augmented path p for this flow from a feasible flow;

The f is adjusted along p, and the new feasible flow is also tried to find the augmented path of it, and the loop is until there is no augmented path;

If f is the lowest cost in a feasible flow of F1, and P is the least expensive augmentation path for all the augmented paths of F;

Then the F is adjusted along p to get the feasible flow _f, which is the lowest cost in all feasible flows of the F1 flow;

So when f is the maximum flow, it is the maximum flow of the minimum cost required;

See code comments.

#include <stdio.h>#include<string.h>#include<queue>#defineINF 99999999using namespacestd;Const intMAXN =1002;structnode{intto ; intF; intCost ; intFlag; intNext;} EDGE[MAXN*maxn/2];intINDEX,HEAD[MAXN],VIS[MAXN],DIS[MAXN],N,PRE[MAXN],FPRE[MAXN];//because the adjacency table directly finds the point and does not find the value, Fpre accesses the index at that point.voidinit () {index=1; memset (Head,-1,sizeof(head));}voidAddintXintYintVintCost ) {edge[index].to=y; EDGE[INDEX].F=v; Edge[index].cost=Cost ; Edge[index].next=Head[x]; Edge[index].flag=index+1; HEAD[X]=index++; Edge[index].to=x; EDGE[INDEX].F=0; Edge[index].cost=-Cost ; Edge[index].next=Head[y]; Edge[index].flag=index-1; Head[y]=index++;}BOOLSPFA (intSintt) {    inti,j; Queue<int>P; memset (PRE,-1,sizeof(pre));//Pre Record pathmemset (Vis,0,sizeof(VIS));  for(i=0; i<=t;i++) Dis[i]=INF; Dis[s]=0; Vis[s]=1; Pre[s]=0;    Q.push (s);  while(!Q.empty ()) {        inttemp=Q.front ();        Q.pop (); Vis[temp]=0;  for(i=head[temp];i!=-1; i=Edge[i].next) {            if(Edge[i].f&&dis[edge[i].to]>dis[temp]+edge[i].cost)//can be relaxed and feasible to flow{dis[edge[i].to]=dis[temp]+Edge[i].cost; if(!Vis[edge[i].to]) {Vis[edge[i].to]=1;                Q.push (edge[i].to); } Pre[edge[i].to]=temp; Fpre[edge[i].to]=i; }        }    }    if(dis[t]>=INF)return false; return true;}voidMCMF (intSintT//Minimum cost flow{    inti,j; intans=0;  while(SPFA (s,t))//the shortest way to find a small fee is whether there is an augmented road    {        intminflow=INF;  for(i=t;i!=0; I=pre[i])//looking for actionable traffic at the lowest cost        {            if(minflow>edge[fpre[i]].f) Minflow=edge[fpre[i]].f; } ans+=MINFLOW*DIS[T];//Fees         for(i=t;i!=0; i=Pre[i]) {EDGE[FPRE[I]].F-=minflow;//the positive reductionEdge[edge[fpre[i]].flag].f+=minflow;//Reverse Add}} printf ("%d\n", ans);}intMain () {inti,j,m;  while(~SCANF ("%d%d",&n,&m)) {init (); intx, Y, Z  for(i=0; i<m;i++) {scanf ("%d%d%d",&x,&y,&z); Add (x, Y,1, z); Add (Y,x,1, z); }        ints=0, t=n+1; Add (S,1,2,0);//source point to starting point capacity is 2 cost 0Add (N,t,2,0);//capacity to meeting point is 2, cost 0MCMF (s,t); }}

poj2135 Minimum cost 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.