QBXT 2018 Spring DP & Graph Theory Class 2018.4.29 --- Shortest Path & Difference Constraints

*Teaching by Mr. Zhong Haoxi*

Common shortest path algorithm:

Floyd → O(n^3)

Bellman-Ford → O(nm)

Dijkstra → O (n ^ 2 + m)

Dijkstra-Heap → O((n+m)log(n+m))

SPFA → O(km) random graph (k is generally 2) Negative loop can be judged → If a point enters the queue more than n times, there is a negative loop.

     O(nm) carefully constructed data

→ Determine which algorithm to use:

if(n<=100) Floyd;

else if (no negative weight) Dijkstra-Heap;

  else SPFA;

①Single-source shortest path can solve the multi-source shortest path problem.

②The shortest path from one point to another point goes through at most n-1 edges.

  →Prove: If the number of edges contained in the path is >=n, it means that there is a cycle, and we can go directly after removing the cycle. Therefore, there is no cycle in the shortest path, and there are at most n-1 edges.

③ The shortest path must have no negative cycle, and the longest path must have no positive cycle. But there can be edges with positive and negative weights.

Secondary short circuit (second shortest simple circuit, i.e. no repeat points):

① Enumerate each edge, delete it and run the shortest path, and finally take min O(n*(n+m)log(n+m)).

②Let dis[u][0] represent the shortest path from the starting point to u, and dis[u][1] represent the second short circuit from the starting point to u.

 

Let W=dis[v][0]+e[i].w

When W ∈ the left set, that is, the newly updated shortest path < the previous shortest path < the previous short circuit ∴ dis[v][1]=dis[v][0], dis[v][0]=W

When W ∈ the intermediate set, that is, the previous shortest path < the newly updated shortest path < the previous short circuit ∴ dis[v][1]=W dis[v][0] does not change

When W ∈ the left set, that is, the previous shortest path < the previous short circuit < the newly updated shortest path ∴ dis[v][1] and dis[v][0] are unchanged

The secondary short circuit at any point may be composed of multiple short circuits, so in the end ans will be updated with the secondary short circuit .

PS. For the second shortest non-simple path, use A* or ZEN (Monte Carlo) algorithm, do not test, and I will not. . . .

K short circuit : (currently not..., but the method is similar to the above)

Difference Constraints:

Given n variables, m constraints such as vi-vj</<=/>/>=/= k, let v1=0, find the minimum/maximum value of vn.

<=>n points, m edges, starting from point v1, ask what is the longest path/shortest path of vn.

Example: v1-v3>=3 ①v1>=v3+3 According to the longest path triangle inequality, v3 connects to v1 with an edge with an edge weight of 3. Run the longest road after building the map

       ②v3<=v1+(-3) According to the shortest-path triangle inequality, v1 connects to v3 an edge with an edge weight of -3. After building the map, run the shortest path,

Maximum value <=> shortest path If there is a negative loop, the maximum value is infinity

Minimum value <=> longest path If there is a positive cycle, the minimum value is infinitesimal

例:{v1-v2>=3 ; v2-v3>=3 ; v1-v3>=2 }

①Maximum value

{v2<=v1+(-3) ; v3<=v2+(-3) ; v3<=v1+(-2)}

∵ The shortest path from v1 to v3 is -6

∴ v3 max=-6

②Minimum value

{v1>=v2+3 ; v2>=v3+3 ; v1>=v3+2}

∵ The longest path from v1 to v3 does not exist

∴v3 min =  -

Common connection methods:

vi-vj>=k -> vi>=vj+k or vj<=vi+(-k)

vi-vj>k -> vi-vj>=k+1

vi-vj=k -> vi-vj>=k&&vi-vj<=k -> vi>=vj+k&&vi<=vj+k Connect two edges to form a zero ring, making the shortest/longest paths equal

vi-vj <= k

vi-vj <k -> vi-vj <= k-1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325374865&siteId=291194637