POJ 2135 Farm Tour

Source: Internet
Author: User
Tags define local

And UVA-1658 admiral like a retreat, is to run a flow of 2 of the minimum cost flow.

Mainly to learn to use Dijkstra processing negative edge to augment, the main idea is to maintain a top mark each point h[v], called V's potential.

For each side ei (u,v) (Forward edge of U to v), fix their Benquan w[i] w ' [i] = W[i] + h[u]-h[v]. Looks a little strange? Don't worry, you can see the role of H later.

The corrected Benquan is guaranteed to be non-negative by taking the value of H appropriately. Take h[x] for the current residual network s to u shortest distance, obviously have h[v]≤h[u] + w[i],w ' [I]≥0, and can

Know only ei on the shortest way, w ' [i] = 0. Specific maintenance methods can be seen in code comments.

Complexity of :

The complexity of augmentation with SPFA (queue-optimized Bellman-ford) is O (f*v*e), which can be reduced to O (f*v^2) or O (F*E*LOGV) with Dijkstra augmentation.

Because the priority queue is actually implemented, the priority cannot be modified, and the container for priority queue overloading is NLOGN for the first vector,vector expansion. (So do not define the priority queue in the loop, pd_ds a heap of black technology in the library)

The actual running time of this problem is even longer than the SPFA of Dijkstra. (As long as it is not the problem of people interested in the card data is generally OK, and the cost flow most people use SPFA bar. )

code: sticker (FOG)

/**********************************************************            ------------------                          ** Author Abyssalfish ***********************************************************/#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<vector>#include<map>#include<Set>#include<algorithm>#include<cmath>using namespacestd;Const intMAXN =2505; template<typename t>structbinaryheap{T HEAP[MAXN]; intsz; #defineCMP (a B) ((a) < (b))//Small    voidPushConstT &x) {inti = + +sz;  while(I >1){            intp = i>>1; if(! CMP (X,heap[p])) Break; Heap[i]= Heap[p];//If complex structures are modified into pointersi =p; } Heap[i]=x; }    voidpop () {T&x = heap[sz--]; inti =1;  while((i<<1) <=SZ) {            intA = i<<1, B = i<<1|1; if(B<=sz && Cmp (Heap[b],heap[a])) A =b; if(! CMP (heap[a],x)) Break; Heap[i]=Heap[a]; I=A; } Heap[i]=x; } T&operator[](intx) {returnheap[x];};Const intMAXV = +, Maxe =4e4;intHd[maxv],to[maxe],nx[maxe],ec,cap[maxe],cost[maxe];#defineeacheage int i = hd[u]; ~i; i = Nx[i]voidAddintUintVintCpintCST) {Nx[ec]=Hd[u]; TO[EC]=v; CAP[EC]=CP; COST[EC]=CST; Hd[u]= ec++;}voidADD (intUintVintCpintCST) {Add (U,V,CP,CST); Add (V,u,0,-CST);}BOOLINQ[MAXV];intDIST[MAXV], PRVE[MAXV];intPOT[MAXV];//PotentialConst intINF =0x3f3f3f3f; typedef pair<int,int>Hnode;#defineFi first#defineSe SecondintVer;//Vertex CountBinaryheapQ;intMincostmaxflow (intSintTintf) {    intCST =0; int*ConstD = Dist, *Constp =Prve; int*Consth = pot;//Maintenance H[u] Indicates the shortest distance from S to u in the current residual network//memset (pot,0,sizeof (int) *ver);     while(f>0) {memset (dist,0x3f,sizeof(int)*Ver); Q.push (Hnode (d[s)=0, s));  while(Q.SZ) {Hnode x= q[1];            Q.pop (); intU =X.second; if(U = =t) {Q.sz=0;  Break; }            if(D[u] < X.first)Continue;  for(eacheage) {intv =To[i]; if(Cap[i] && d[v] > D[u] + cost[i] + h[u]-h[v]) {//Edge Distance correction u->v, according to H definition, h[v] <= w[e] + h[u]D[V] = D[u] + cost[i] + h[u]-h[v];//D[v] = d ' [v] (actual value)-h[v] (H[s] = 0) on shortest path correctionP[V] =i;                Q.push (Hnode (d[v], v)); }            }        }        if(D[t] = = INF) Break;  for(inti =0; i < Ver; i++) H[i] + = D[i];//if d[i] = INF???        /*in this subject a = = 1 int a = f;//augment flow for (int v = t, e; v! = s; v = to[e^1]) {e = P[v];        if (Cap[e] < a) a = Cap[e]; }        */CST+ = H[t];//A*h[t]F--;//- = a         for(intv = t, E; V! = s; v = to[e^1]) {e=P[v]; Cap[e]--;//- = A;cap[e^1] ++;//- = A;        }    }    returnCST;}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif    intN, M; scanf"%d%d",&n,&m); Ver=N; memset (HD,0xFF,sizeof(int)*Ver);  for(inti =0; I < m; i++){        intA,b,c; scanf"%d%d%d",&a,&b,&c); ADD (--a,--B,1, c); ADD (B,a,1, c); } printf ("%d\n", Mincostmaxflow (0, N-1,2)); return 0;}

POJ 2135 Farm Tour

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.