Short-Circuit Algorithm template collection (Dijkstar,dijkstar (priority queue Optimization), multi-source shortest path Floyd)

Source: Internet
Author: User
Tags cmath

Before we start, let's get to the basics of simple graph theory.

Save the diagram:

1. Adjacency matrix. G[MAXN][MAXN];

2. adjacency table

adjacency table We have two different ways

(1) vector< Node > G[MAXN];

This is the previous definition of the size of the diagram, and then use the following when you do not have to apply for the size of the diagram, but because it is directly applied to the size

The diagram is initialized so that it may be used in some of the topics to time out

(2) vector< vector<node> > G;

This is an undefined size, but a request is made for the size of the memory before it is used.

G.resize (n+1);

Dijkstra ' s algorithm

Algorithm idea:

1. From the source point to the source point all the points can be reached at the distance of the update, and then from all points except the source point to find the closest point from the source point.

2. Then update all connected points of the shortest point we found before, but require that this point has not been treated as a minimum point

3. Repeat the above operation N Times.

Single source Shortest path we can also prioritize him. The following is a HDU2544 template with Dijkstra ' s algorithm

Adjacency matrix version without priority queue optimization

#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include < Vector> #include <queue>using namespace std; #define INF 0xfffffff#define maxn 1002int g[maxn][maxn];//save diagram int    dist[maxn];//represents the distance from the starting point to point I, BOOL vis[maxn];//, to determine whether this point has been visited by int m, the number of n;//edge m vertices nvoid Init () {for (int i=0; i<=n; i++)        {Vis[i] = false;        Dist[i] = INF;    for (int j=0; j<=i; j + +) G[i][j] = g[j][i] = INF;    }}int dij (int star,int end)//Start---End point {Dist[star] = 0;        for (int i=1; i<=n; i++) {int index = 0, Min = INF;                for (int j=1; j<=n; J + +) {if (!vis[j] && Min > Dist[j])//Find out the points that have not been visited and are closest to the beginning        Min = dist[j], index = j;        } Vis[index] = true; for (int j=1; j<=n; j + +)//update all unreachable point distances so that they become the nearest point {if (!vis[j] && dist[j] > Dist[index] + G [Index]        [j]) dist[j] = Dist[index] + g[index][j];  }    }  return dist[end];}        int main () {while (CIN >> n >> m, M + N) {Init ();        int A, b, C;            for (int i=0; i<m; i++) {cin >> a >> b >> c;            G[a][b] = min (g[a][b], c);        G[b][a] = g[a][b];        } int ans = DIJ (1,n);    cout << ans << endl; } return 0;}

Next is the adjacency table version, which uses the priority queue optimization

1#include <iostream>2#include <cmath>3#include <cstring>4#include <cstdlib>5#include <cstdio>6#include <algorithm>7#include <vector>8#include <queue>9 using namespacestd;Ten #defineINF 0XFFFFFFF One #defineMAXN 1002 A  - structNode - { the     inte; -     intW; -FriendBOOL operator<(node A, node B) -     { +         returnA.W >B.W; -     } + }; A  at BOOLVIS[MAXN]; -  - intm, N; -vector< vector<node> >G; -  - intDij (intStar,intEnd) in { - Node P, Pn; toP.E =Star; +P.W =0; -  thePriority_queue<node>Q; *  $ Q.push (P);Panax Notoginseng  -      while( !q.empty ()) the     { +P =q.top (); A Q.pop (); the  +         if(VIS[P.E]) -             Continue; $  $VIS[P.E] =true; -  -         if(P.E = =End) the             returnP.W; - Wuyi         intLen =g[p.e].size (); the  -          for(intI=0; i< Len; i++) Wu         { -PN.E =g[p.e][i].e; AboutPN.W = G[P.E][I].W +P.W; $  -             if( !VIS[PN.E]) - Q.push (Pn); -         } A     } +     return-1; the } -  $ intMain () the { the Node P; the      while(Cin >> n >> m, m+N) the     { - g.clear (); inG.resize (n+1); the  thememset (Vis,false,sizeof(Vis)); About  the          for(intI=0; i<m; i++) the         { the             intA, B, C; +Cin >> a >> b >>C; -P.E =b; theP.W =C;Bayi G[a].push_back (P); theP.E =A; the G[b].push_back (P); -         } -  the         intAns = DIJ (1, n); the  thecout << ans <<Endl; the     } -     return 0; the}

Here is the Floyd algorithm

Floyd is to find the shortest path of multiple sources, so as to achieve a short circuit between all point pairs

This algorithm just notice two o'clock on the line, initialize the time g[i][i] = 0, the other initialized to the INF

#include <iostream>#include<cmath>#include<cstring>#include<cstdlib>#include<cstdio>#include<algorithm>#include<vector>#include<queue>using namespacestd;#defineINF 0XFFFFFFF#defineMAXN 1002intG[MAXN][MAXN];intDIST[MAXN][MAXN];intm, N;voidFloyd () { for(intk=1; k<=n; k++)    {         for(intI=1; i<=n; i++)        {             for(intj=1; j<=n; J + +) {G[i][j]= Min (G[i][j], g[i][k] +G[k][j]); }        }    }}voidInit () { for(intI=0; i<=n; i++) {G[i][i]=0;  for(intj=0; j<i; J + +) G[i][j]= G[j][i] =INF; }}intMain () { while(Cin >> n >> m, m+N) {Init ();  for(intI=0; i<m; i++)        {            intA, B, C; CIN>> a >> b >>C; G[A][B]=min (g[a][b],c); G[b][a]=G[a][b];        } Floyd (); cout<< g[1][n] <<Endl; }    return 0;}

Short-Circuit Algorithm template collection (Dijkstar,dijkstar (priority queue Optimization), multi-source shortest path Floyd)

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.