Graph theory $\cdot$ Shortest circuit problem

Source: Internet
Author: User

    • Dijkstra single source Shortest path algorithm

The Dijkstra can calculate the shortest path from the starting point to each point, and the single source minimum (SSSP). This feature makes Dijkstra often used for preprocessing of other algorithms. The code to calculate the shortest path with the Dijkstra algorithm is as follows:

Note: Code comments See the Getting started with algorithmic competition-training Guide (Rujia)

1 structdijkstra{2     intN, M;3Vector<e>e;4vector<int>G[MAXN];5     BOOLDONE[MAXN];6     intD[MAXN];7     intP[MAXN];8     voidInitintN) {9          This->n =N;Tenfor (I,0N1) g[i].clear (); One e.clear (); A     } -     voidAdde (int  from,intTo,intDist) { -E.PB (E ( from, to, Dist)); them =e.size (); -g[ from].PB (M-1); -     } -     voidDijkstraints) { +Priority_queueQ; -for (I,0N1) D[i] =Int_inf; +D[s] =0; ACLR (Done,0); atQ.push (Heapnode (0, s)); -          while(!Q.empty ()) { -Heapnode x =Q.top (); Q.pop (); -             intU =x.u; -             if(Done[u])Continue; -Done[u] =1; in             intSZ =g[u].size (); -for (I,0, Sz-1){ toE &y =E[g[u][i]]; +                 if(D[y.to] > D[u] +y.dist) { -D[y.to] = D[u] +y.dist; theP[y.to] =G[u][i]; * Q.push (Heapnode (d[y.to], y.to)); $                 }Panax Notoginseng             } -         } the     } +};
    • Bellman-ford algorithm

An important application of the Bellman-ford algorithm is to award negative loops. If the relaxation (relax) operation can be performed after the iteration $n-1$, there must be a negative circle. If the queue is implemented, then when a node is enqueued $n$ times can be determined that there is a negative circle, the code is as follows:

1 structbellman_ford{2     intN, M;3Vector<e>e;4vector<int>G[MAXN];5     BOOLINQ[MAXN];6     intD[MAXN];7     intP[MAXN];8     intCNT[MAXN];9     voidInitintN) {Ten          This->n =N; Onefor (I,0N1) g[i].clear (); A e.clear (); -     } -     voidAdde (int  from,intTo,intDist) { theE.PB (E ( from, to, Dist)); -m =e.size (); -g[ from].PB (M-1); -     } +     BOOLNegcyc () { -queue<int>Q; +CLR (INQ,0), CLR (CNT,0); Afor (I,0N1) D[i] =0, inq[i] =1, Q.push (i); at          while(!Q.empty ()) { -             intU =Q.front (); Q.pop (); -Inq[u] =0; -             intSZ =g[u].size (); -for (I,0, Sz-1){ -E &y =E[g[u][i]]; in                 if(D[y.to] > D[u] +y.dist) { -D[y].to = D[u] +y.dist; toP[e.to] =G[u][i]; +                     if(!Inq[y.to]) { - Q.push (y.to); theInq[y.to] =1; *                         if(++cnt[y.to] > N)return 1; $                     }Panax Notoginseng                 } -             } the         } +         return 0; A     } the};

Graph theory $\cdot$ Shortest circuit problem

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.