Dijkstra's shortest path algorithm

Dijkstra's algorithm: seeking single source shortest path is the right in FIG.

1, the optimized code heap FIG Dijkstra:

 1 #define INF 0x7fffffff
 2 #include<queue>
 3 #include<cstdio>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 const int N = 100005;
 8 const int M = 500005;
 9 struct edge
10 {
11     int to,dis,next;
12 }e[M];
13 int head[N],dis[N],cnt;
14 bool vis[N];
15 int n,m,s;
16 void add(int u,int v,int d)
17 {
18     ++cnt;
19     e[cnt].dis=d;
20     e[cnt].to=v;
21     e[cnt].next=head[u];
22     head[u]=cnt;
23 }
24 struct node
25 {
26     int dis,pos;
27 };
28 bool operator < (const node &x,const node &y)
29 {
30     return x.dis<y.dis;
31 }
32 priority_queue<node> q;
33 inline void dijkstra()
34 {
35     dis[s]=0;
36     q.push((node){0,s});
37     while(!q.empty())
38     {
39         node tmp=q.top();
40         q.pop();
41         int x=tmp.pos,d=tmp.dis;
42         if(vis[x]) continue;
43         vis[x]=1;
44         for(int i=head[x];i;i=e[i].next)
45         {
46             int y=e[i].to;
47             if(dis[y]>dis[x]+e[i].dis)
48             {
49                 dis[y]=dis[x]+e[i].dis;
50                 if(!vis[y]) q.push((node){dis[y],y});
51             }
52         }
53     }
54 }
55 int main()
56 {
57     cin>>n>>m>>s;
58     for(int i=1;i<=n;++i) dis[i]=INF;
59     for(int i=0;i<m;++i)
60     {
61         int u,v,d;
62         cin>>u>>v>>d;
63         add(u,v,d);
64     }
65     dijkstra();
66     for(int i=1;i<=n;++i)
67     cout<<dis[i]<<" ";
68     cout<<endl;
69     return 0;
70 }
View Code

2, no stack optimized code to FIG Dijkstra:

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<vector>
 4 #include<queue>
 5 using namespace std;  
 6 const int N = 10005;  
 7 const int INF = 0x7fffffff;  
 8 struct node
 9 {  
10     int x,d;  
11     node(int a,int b){x=a;d=b;}
12 };  
13 bool operator <(const node &x,const node &y)
14 {
15     return x.d<y.d;
16 }
17 vector<node> eg[N];  
18 int dis[N],n;  
19 void Dijkstra(int s)  
20 {  
21     int i;  
22     for(i=0;i<=n;i++) dis[i]=INF;  
23     dis[s]=0; 
24     priority_queue<node> q;  
25     q.push(node(s,dis[s]));  
26     while(!q.empty())  
27     {  
28         node x=q.top();q.pop();  
29         for(i=0;i<eg[x.x].size();i++)  
30         {  
31             node y=eg[x.x][i];  
32             if(dis[y.x]>x.d+y.d)  
33             {  
34                 dis[y.x]=x.d+y.d;  
35                 q.push(node(y.x,dis[y.x]));  
36             }  
37         }  
38     }  
39 }  
40 int main()  
41 {  
42     int a,b,d,m; 
43     cin>>n>>m>>s;
44     //处理多组数据时,使用  for(int i=0;i<n;++i) eg[i].clear();  
45     while(m--)  
46     {  
47         cin>>a>>b>>d;
48         eg[a].push_back(node(b,d));  
49         eg[b].push_back(node(a,d));  
50     }  
51     Dijkstra(s);  
52     cout<<dis[n]<<endl;
53     return 0;  
54 }
View Code

3, entitled: Luogu shortest template 1  Luogu shortest template 2  Luogu shortest template 3

* When looking solution to a problem and found a big brother to use zkw Dijkstra's algorithm to optimize the tree line , where the code and paste the link:

  1 #include <algorithm>
  2 #include <cmath>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <ctime>
  7 #include <iostream>
  8 #include <map>
  9 #include <queue>
 10 #include <set>
 11 #include <stack>
 12 #include <string>
 13 #include <vector>
 14 using namespace std;
 15 #define go(i, j, n, k) for (int i = j; i <= n; i += k)
 16 #define fo(i, j, n, k) for (int i = j; i >= n; i -= k)
 17 #define rep(i, x) for (int i = h[x]; i; i = e[i].nxt)
 18 #define mn 100010
 19 #define mm 200020
 20 #define inf 2147483647
 21 #define ll long long
 22 #define ld long double
 23 #define fi first
 24 #define se second
 25 #define root 1, n, 1
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define bson l, r, rt
 29 inline int read(){
 30     int f = 1, x = 0;char ch = getchar();
 31     while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
 32     while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
 33     return x * f;
 34 }
 35 inline void write(int x){
 36     if (x < 0)putchar('-'),x = -x;
 37     if (x > 9)write(x / 10);
 38     putchar(x % 10 + '0');
 39 }
 40 //This is AC head above...
 41 struct node{
 42     int v, nxt, w;
 43 } e[mm << 1];
 44 int h[mn], p;
 45 inline void add(int a,int b,int c){
 46     e[++p].nxt = h[a];
 47     h[a] = p;
 48     e[p].v = b;
 49     e[p].w = c;
 50 }
 51 int dis[mn];
 52 int n, m, s, t;
 53 struct tree{
 54     int minw, minv;
 55 };
 56 is  struct SegmentTree {
 57 is      Tree Z [Mn << 2 ];
 58      inline void Update ( int RT) {
 59          Z [RT] .minw min = (Z [RT << . 1 ] .minw, Z [RT << . 1 | . 1 ] .minw); // maintain minimum interval 
60          Z [RT] .minv = (Z [RT << . 1 ] .minw <Z [RT << . 1 | . 1 ?] .minw) Z [RT < < . 1 ] .minv: Z [RT << . 1 | . 1 ] .minv; // maintain the position of the minimum interval 
61      }
 62     inline void Build ( int L, int R & lt, int RT) { // Contribution 
63 is          IF (L == R & lt) {
 64              Z [RT] .minw == S = L? 0 ; INF: // We direct contribution the point s is set to 0 
65              Z [RT] = L .minv; // record the minimum position, to facilitate the modification 
66              return ;
 67          }
 68          int m = (L + R & lt) >> . 1 ;
 69          Build (LSON);
 70          Build (rson);
 71 is          Update (RT);
 72      }
 73     inline void modify(int l,int r,int rt,int now,int v){//单点修改
 74         if(l==r){
 75             z[rt].minw = v;
 76             return;
 77         }
 78         int m = (l + r) >> 1;
 79         if(now<=m)
 80             modify(lson, now, v);
 81         else
 82             modify(rson, now, v);
 83         Update (RT);
 84      }
 85  } TR;
 86 inline void Dij of () { // the core of Dijkstra's 
87      Go (I, . 1 , n-, . 1 ) {
 88          dis [I] = INF;
 89      } // initialize dis 
90      DIS [S] = 0 ;
 91 is      the while (tr.z [ . 1 ] .minw <INF) { // this is determined whether the air 
92          int X = tr.z [ . 1 ] .minv; // get the entire segment tree smallest point 
93          tr.modify (the root, X, INF); //Single point to modify the minimum point INF 
94          REP (I, X) {
 95              int V = E [I] .v;
 96              IF (DIS [V]> DIS [X] + E [I] .W) {
 97                  DIS [V] = DIS [X] + E [I] .W;
 98                  tr.modify (the root, V, DIS [X] + E [I] .W); // this is similar to enqueue operation 
99              }
 100          }
 101      }
 102  }
 103  int main () {
 104      n-= Read (), m = Read (), S = Read (), T = Read ();
 105      Go (I, . 1 , m, . 1 ) {
 106          intRead = X (), Y = Read (), V = Read ();
 107          the Add (X, Y, V);
 108          the Add (Y, X, V); // this must remember not to be positive in FIG. anti two sides 
109      }
 110      tr.build (the root); // achievements 
111      Dij of (); // the Dijkstra 
112      COUT << DIS [T];
 113      return  0 ;
 114 }
Social segment tree optimization

Links

Guess you like

Origin www.cnblogs.com/cptbtptpbcptbtptp/p/11299504.html