HDU4067 Random Maze (minimum cost maximum flow)

Source: Internet
Author: User

The title probably said, to a picture, delete some one-way edge, so that the starting point s out of the degree of more than 1, the end point T into the degree of more than 1, other points out of the degree equal to the degree. Where the cost of removing the edge is bi, the cost of preserving the edge is AI, and the minimum cost of asking for completion is how much.

At first I thought of a similar construction method with a mixed-graph Euler loop (POJ1637):

    • Assuming that all edges are retained at the beginning, the degrees and degrees of each point are calculated, and the degree of -1,t points of the S point is 1;
    • The point source point with the degree-to-degree equals positive number is then connected to an edge whose capacity is a degree-to-degree, and a point equal to a negative number that is connected to the meeting point with a capacity of degrees-out edge
    • This is to remove the edges so that those connected to the Yuanhui edge full flow, so that the requirements of the various points, and delete an edge <u,v> will make U -1,v the degree of the degree of 1, so that in the capacity network by U to v capacity of 1 cost Bi-ai edge
    • ΣAI-MCMF is the answer.

But this is wrong = = and because of the negative loop dead loop tle. I tried to remove the negative ring, found that both ends of the point and the source or sink connected to the edges are not working, feel strange seems not very right, the submission is WA.

The right thing to do is not to assume that all edges are reserved at first, but to be greedy to first determine the minimum cost, that is, if AI<BI is retained or deleted!

And then the next thing is similar, I will not repeat it.

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineINF (1&LT;&LT;30)7 #defineMAXN 1118 #defineMAXM 88889 structedge{Ten     intU,v,cap,cost,next; One }EDGE[MAXM]; A intVS,VT,NV,NE,HEAD[MAXN]; - voidAddedge (intUintVintCapintCost ) { -Edge[ne].u=u; Edge[ne].v=v; Edge[ne].cap=cap; edge[ne].cost=Cost ; theEdge[ne].next=head[u]; head[u]=ne++; -Edge[ne].u=v; Edge[ne].v=u; edge[ne].cap=0; edge[ne].cost=-Cost ; -EDGE[NE].NEXT=HEAD[V]; head[v]=ne++; - } + intD[MAXN],PRE[MAXN]; - BOOLVIS[MAXN]; + BOOLSPFA () { A      for(intI=0; i<nv; ++i) { atD[i]=inf; vis[i]=0; -     } -d[vs]=0; vis[vs]=1; -queue<int>que; - Que.push (VS); -      while(!Que.empty ()) { in         intu=Que.front (); Que.pop (); -          for(intI=head[u]; i!=-1; I=Edge[i].next) { to             intv=edge[i].v; +             if(Edge[i].cap && d[v]>d[u]+edge[i].cost) { -d[v]=d[u]+Edge[i].cost; thepre[v]=i; *                 if(!Vis[v]) { $vis[v]=1;Panax Notoginseng Que.push (v); -                 } the             } +         } Avis[u]=0; the     } +     returnd[vt]!=INF; - } $ inttot; $ intMCMF () { -     intres=0, mxflow=0; -      while(SPFA ()) { the         intflow=inf,cost=0; -          for(intU=VT; U!=vs; u=edge[pre[u]].u) {Wuyiflow=min (flow,edge[pre[u]].cap); the         } -mxflow+=flow; Wu          for(intU=VT; U!=vs; u=edge[pre[u]].u) { -edge[pre[u]].cap-=flow; Aboutedge[pre[u]^1].cap+=flow; $cost+=flow*Edge[pre[u]].cost; -         } -res+=Cost ; -     } A     if(Tot!=mxflow)returnINF; +     returnRes; the } - intu[2222],v[2222],w1[2222],w2[2222]; $ intDEG[MAXN]; the intMain () { the     intt,n,m,s,t; thescanf"%d",&T); the      for(intCse=1; cse<=t; ++CSE) { -scanf"%d%d%d%d",&n,&m,&s,&t); in         intres=0; thememset (deg,0,sizeof(deg)); the--deg[s]; ++Deg[t]; About          for(intI=0; i<m; ++i) { thescanf"%d%d%d%d", u+i,v+i,w1+i,w2+i); the             if(w1[i]<W2[i]) { the++Deg[u[i]]; +--Deg[v[i]]; -res+=W1[i]; the}Else{Bayires+=W2[i]; the             } the         } -tot=0; -vs=0; vt=n+1; nv=vt+1; Ne=0; thememset (head,-1,sizeof(head)); the          for(intI=1; i<=n; ++i) { the             if(deg[i]>0) Addedge (Vs,i,deg[i],0), tot+=Deg[i]; the             ElseAddedge (I,vt,-deg[i],0); -         } the          for(intI=0; i<m; ++i) { the             if(w1[i]<W2[i]) { theAddedge (U[i],v[i],1, w2[i]-w1[i]);94}Else{ theAddedge (V[i],u[i],1, w1[i]-w2[i]); the             } the         }98         inttmp=MCMF (); About         if(Tmp==inf) printf ("Case %d:impossible\n", CSE); -         Elseprintf"Case %d:%d\n", cse,res+tmp);101     }102     return 0;103}

HDU4067 Random Maze (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.