POJ 2135 Farm Tour [minimum cost maximum flow]

Source: Internet
Author: User

Test instructions

There are n points and m bars, allowing you to start from 1 to N and then back from N to 1, without requiring all points to pass, but each side can only walk once. The Edge is a non-forward edge.

Ask the shortest distance to walk.

At the beginning of the problem has not been engaged in the cost of the flow, and later made to come back to see, think of the plan is not difficult to build, because to ensure that each side can only walk once, then we split the side into two points, a starting point and the end, the capacity is 1, the weight is the length of the road. Then the two endpoints to the starting point of the connection capacity is 1 weight is 0 of the edge, the end point to two endpoints, respectively, the capacity is 1 weight is 0 of the edge, from the source point to 1 of the capacity 2 weight 0 of the edge, from N to the meeting point with a capacity of 2 weight 0 edge.

#include <stdio.h>#include<queue>#defineMAXN 55000#defineMAXM 20002*5#defineINF 0x3f3f3f3fusing namespacestd;//start number must be minimum, end number must be maximumBOOLVIS[MAXN];//whether the record in the SPFA is in the queuestructedge{Edge*next,*op;//op is pointing to the opposite side    intT,c,v;//T next point number, C capacity, V weight}ES[MAXM],*V[MAXN];//es edge static adjacency table, V-point numberintn,m,s,t,ec=-1;//s source point minimum, T meeting point Max, EC current edge numberintDEMOND[MAXN],SP[MAXN],PREV[MAXN];//record distance in SPSPFA, prev record previous point pathEdge *PATH[MAXN];//Synchronize records with Prev, record to previous edgevoidAddedge (intAintBintVintC=INF) {Edge E1={v[a],0, B,c,v},e2={v[b],0A0,-v}; es[++ec]=e1; v[a]=&Es[ec]; es[++ec]=e2; v[b]=&Es[ec]; V[a]->op=v[b]; v[b]->op=v[a];}voidinit () {intn,m; scanf ("%d%d",&n,&m); S=0; t=n+m*2+1; EC=-1; Addedge (S),1,0,2); Addedge (N,t,0,2); intA,b,c;  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&c); Addedge (A,n+i,0,1); Addedge (B,n+i,0,1); Addedge (n+m+i,a,0,1); Addedge (n+m+i,b,0,1); Addedge (n+i,n+m+i,c,1); }}BOOLSPFA () {intu,v;  for(u=s;u<=t;u++) {Sp[u]=INF; } Queue<int>Q; Prev[s]=-1;    Q.push (S); Sp[s]=0; Vis[s]=1;  while(!Q.empty ()) {u=Q.front (); Vis[u]=0;        Q.pop ();  for(Edge *k=v[u];k;k=k->next) {v=k->T; if(k->c>0&&sp[u]+k->v<Sp[v]) {Sp[v]=sp[u]+k->v; PREV[V]=T; PATH[V]=K; if(vis[v]==0) {Vis[v]=1;                Q.push (v); }            }        }    }    returnsp[t]!=INF;}intargument () {inti,cost=inf,flow=0; Edge*e;  for(i=t;prev[i]!=-1; i=Prev[i]) {e=Path[i]; if(e->c<cost) cost=e->C; }     for(inti=t;prev[i]!=-1; i=Prev[i]) {e=Path[i]; E->c-=cost;e->op->c+=Cost ; Flow+=e->v*Cost ; }    returnflow;}intMaxcostflow () {intflow=0;  while(SPFA ()) {Flow+=argument (); }    returnFlow;}intMain () {init (); printf ("%d\n", Maxcostflow ()); return 0;}

POJ 2135 Farm Tour [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.