Luo Gu P4779 [[template] Single Source Shortest Path (Standard Edition)]

Is the right map, seemingly appears to be a bare $ dijkstra $

## $ dijkstra $ main steps:

First, $ dijkstra $, the source point represents the beginning of a departure point, the blue dot indicates the point has not been determined, the white dots represent points have been identified.

The first step is to determine the source, sometimes the title will tell you.

Next a second step, to update the current point through the shortest distance to the point that it can, and marked as the white point.

The third step, after once again looking for a Saowan currently has not been used before and closest to the source of the blue dot, do the next update.

## $ dijkstra $ simulation process:

The following figure as an example:

![](https://cdn.luogu.com.cn/upload/pic/64035.png)

At first, all points are blue dots. $ Dis [1] = 0 $, the remaining points are $ 0x7f $.

![](https://cdn.luogu.com.cn/upload/pic/64037.png)

The first round found $ DIS [1] $ minimum, so that the $ 1 $ marked as white spots, get new $ dis [2] = 2 $, $ dis [3] = 4 $, $ dis [4] = 7 $

![](https://cdn.luogu.com.cn/upload/pic/64039.png)

The second round DIS find $ [2] $ minimum, so that the $ 2 $ marked white spot, obtain a new $ dis [3] = 3 $, $ dis [5] = 4 $

![](https://cdn.luogu.com.cn/upload/pic/64041.png)

Found DIS third round $ [3] $ minimum, it will be $ 3 $ marked white spot, obtain a new $ dis [4] = 4 $

![](https://cdn.luogu.com.cn/upload/pic/64043.png)

The last two cycles and then point $ 4 $ 5 $ $ point to become white point.

So the final $ dis $ array:

```cpp
1 2 3 4 5
dis 0 2 3 4 4
```


## $ dijkstra $ program implementation:

Finally, this question requires a source point to each point of the shortest distance, obviously then run over $ dijkstra $ $ dis $ output array can be.

Simple algorithm because every time to find the current from the source point closest blue dot, the time complexity will reach $ n ^ 2 $, the data can not be over this question, but still can put [this question] (https: // www. luogu.org/problem/P3371)$A$ out.

So we have to think how to optimize, it is easy to think of a way is to use a priority queue to maintain all current point distance from the source point, every pop shortest, until the blue dot far. This eliminates the need $ O (n) $ to find the total time complexity $ O (n + m $ $ log $ $ n) $.

Note that the comparison is less than the structure overloaded operators Oh $ qwq $.

$Code:$

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int n-, m, S, DIS [ 100010 ], VIS [ 100010 ];
 struct Node {
     int X, DIS;
     BOOL  operator <( const Node & A) const { // weight contained less than operators 
        return DIS> a.dis;
    }
};
vector< pair<int,int> >next[100010];//存边 
priority_queue<node>q;
void dijkstra(){
    Memset (DIS, 0x7F , the sizeof (DIS)); // start all values are set to the maximum 
    DIS [S] = 0 ;
    q.push ({S, 0 }); // source 
    the while (! q.empty ()) {
         int Min = q.top () X;. // shortest per pop 
        q.pop (); / / dequeue 
        IF (VIS [Min]) Continue ; // can not be white spots 
        VIS [Min] = . 1 ; // tag 
        for ( int I = 0 ; I <Next [Min] .size (); I ++ ) {
             int = next the next [Min] [I] .first; /// / next point obtained 
            int Nextw next = [Min] [I] .second; // get the right side 
            IF (DIS [the next]> DIS [Min] +Nextw){
                dis[Next]=dis[Min]+Nextw;//更新 
                q.push({Next,dis[Next]});//入队 
            }
        }
    }
}
int main () {
    cin>>n>>m>>s;
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        next[x].push_back(make_pair(y,z));//连边 
    }
    dijkstra();
    for(int i=1;i<=n;i++){
        cout<<dis[i]<<" ";
    }
    cout<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Agonim/p/11789530.html