[POJ2135] Minimum cost maximum flow

Source: Internet
Author: User

has been delayed for some reason ... Minimum cost maximum flow did not engage.

Today while personal status is good, hurry to see, sure enough 30min not to see the algorithm + template and a dropped a problem.

Feel the minimum cost maximum flow after learning the maximum flow is still very good understanding. Find the minimum spending scheme that flows 1 units of traffic from the start to the end, and then update the data.

Keep looking for augmented road, constantly accumulate the answer, keep approaching the optimal solution.

There is no question of understanding. The code was written once and it went well.

POJ2135

is actually a template problem that is not so easy to set.

The problem of network flow is re-modeled. This is the problem.

The shortest path from the start to the end of the round, but not the same edge.

A round trip is equivalent to walking two times from the beginning to the end.

At first glance feel very similar to the number of squares, and then think of the recent a dropped k to take the number of squares, is to use the network stream algorithm. So I thought about the network flow.

We use traffic to control each edge only once, and then add a source point and a meeting point, respectively, the 0,n flow is 2.

This model is built!

But just think of a problem, back and forth two times the side has given 1 of the traffic, so not to go back and forth each time?

That is the case.

Red-blue-green-yellow is a well-taken path.

When the orange side costs close to the positive infinity, the orange dots will go to green-blue-violet-yellow

This goes through the green side two times.

But it was soon discovered that such a situation did not exist.

It is possible to take red-blue-violet + red-orange-green-yellow, which also reduces the cost of two green edges. The cost of the subject is >0, so we can see that there is no such situation.

So the code can be assured to knock up!

(I would like to do some of the minimum cost of the maximum flow of practice code proficiency, but because of the time is tight ...) Suddenly found today is Friday, the provincial election Monday report ... The original impression of three or four days did not expect to be the weekend qaq and then learned that tomorrow afternoon is also a holiday so there is no

Time to prepare! So given these reasons ... Let's leave some time to review the unskilled things.

Program poj2135;Constmaxn=1010; maxm=50010;varN,m,i,j,x,y,z:longint; link,opt,dis,pre:array[-1..      Maxn]of Longint; vis:array[-1..      Maxn]of Boolean; FA, from, next,w,c,rev:array[-1.. Maxm]of longint;function min (a,b:longint): Longint;beginifA<b then exit (a)Elseexit (b); end;procedure Add (X,y,z,cc:longint); Begin Inc (J); Fa[j]:=y; from[j]:=x;next[j]:=link[x];link[x]:=j;w[j]:=z;c[j]:=cc;rev[j]:=j+1; Inc (J); Fa[j]:=x; from[j]:=y;next[j]:=link[y];link[y]:=j;w[j]:=-z;c[j]:=0; rev[j]:=j-1; end; function Spfa:boolean;varhead,tail,x,j:longint;begin fillchar (DIS,sizeof(DIS), the); Fillchar (Vis,sizeof(VIS),true); Head:=0; tail:=1; opt[1]:=0; vis[0]:=false;d is[0]:=0;  whileHead<>tail DoBegin head:= (head+1) mod maxn; X:=opt[head];j:=Link[x];  whileJ<>0  Dobeginif(c[j]>0) and (dis[x]+w[j]<Dis[fa[j]] then BEGIN DIS[FA[J]]:=dis[x]+W[j]; PRE[FA[J]]:=J; ifVis[fa[j]] then BEGIN tail:= (tail+1) mod maxn; Opt[tail]:=Fa[j];            End            End J:=Next[j];        End VIS[X]:=true;    End ifdis[n+1]<>dis[-1] Then exit (true)ElseExitfalse); end;function Mcmf:longint;varans,x,sum:longint;begin ans:=0;  whileSpfa DoBegin sum:=Maxlongint; X:=n+1;  whileX<>0  DoBegin sum:=min (sum,c[pre[x]]); X:= from[Pre[x]];        End X:=n+1;  whileX<>0  DoBegin Inc (Ans,sum*W[pre[x]]);            Dec (c[pre[x]],sum);            Inc (C[rev[pre[x]]],sum); X:= from[Pre[x]];    End    End Exit (ANS); End;begin assign (input,'poj2135.in'); reset (input); READLN (N,m); J:=0;  fori:=1to M Dobegin READLN (x, y, z); Add (x, Y, Z,1); Add (Y,x,z,1);    End Add (0,1,0,2); Add (n,n+1,0,2); Writeln (MCMF); end.

  

[POJ2135] 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.