Dijkstra's algorithm summary

1. For the shortest path algorithm method we use is the most commonly used Dijkstra algorithm to solve, but in the shortest path algorithm often some variant of Dijkstra's algorithm, but the core is the need to solve the same from the source s to vertex v shortest path, appearing in the title more is another case that the path is the origin to the destination of more than one , and then there are two paths to reach the shortest distance above the subject will be given a second scale (second a distance scale) required to select a second scale optimal shortest paths in all path, the second scale is about three kinds of common methods or a combination of the topic

2 are as follows:

① to each edge to add a right side (for example, cost) and then ask the requirement to spend on the path of a number of shortest paths and minimum (if the other side is defined rights can also be the biggest)

② add a point right (for example, each city can collect supplies) to each point, then the shortest path when a number of points on the right path and the requirements of the largest (if it is right to point other definitions can be It is the smallest)

③ directly asked how many shortest paths

For the topic of these three methods, we only need to add an array to store the new edge weight or number of points right or shortest path, Dijkstra then be modified to optimize the steps d [v] is to algorithmically, other parts do not need to be modified

3. For the following three methods explanation is given topic code modifications:

① edge weight increase

Representative additional cost to the right side for example, represented by cost [u] [v] u-> v cost (by a title input) and adds an array C [], so that from the start point reaches the vertex u s takes a minimum of when c [u], initialization of only c [s] = 0, the remaining c [v] is INF, so that you can d [u] + G [u] [v] <d [v] update D [ v] and c [v], and when d [u] + G [u] [v] == d [v], and c [u] + cost [u] [v] <c [v] (i.e., may be s v reaches a minimum when the update takes better) c [v], the following code:

for(int v = 0; v < n; v++){
	if(vis[v] == false && G[u][v] != INF){
		if(d[u] + G[u][v] < d[v]){
			d[v] = d[u] + G[u][v];
			c[v] = c[u] + cost[u][v];
		}else if(d[u] + G[u][v] == d[v] && c[u] + cost[u][v] < c[v]){
			c[v] = c[u] + cost[u][v];
		}
	}
} 

② new point right

The new material right to the point on behalf of the city can receive, for example, represents the number of goods in the city u (by the title input) and adds an array w [] by weight [u], initialization time only w [s] is weight [s], the remaining w [u] is 0, so that you can when d [u] + G [u] [v] <d [v] updating d [v] and w [v], when when d [u] + G [u] [v] == d [v], and c [u] + cost [u] [v]> [v] (i.e. the maximum number of materials may be more optimal s reaches v) update w [v], the following code:

for(int v = 0; v < n; v++){
	if(vis[v] == false && G[u][v] != INF){
		if(d[u] + G[u][v] < d[v]){
			d[v] = d[u] + G[u][v];
			w[v] = w[u] + weight[u][v];
		}else if(d[u] + G[u][v] == d[v] && w[u] + weight[v] < w[v]){
			w[v] = w[u] + weight[v];
		}
	}
} 

Solving the number of shortest path ③

Only you need to add an array NUM [], so that from the starting point s shortest path to the number of vertex v is num [u], while initializing only the NUM [s] is 1, num [u] is 0, so that you can when d [u] + G [u] [v] <d [v] updating d [v], and let the num [v] Inherited from num [u], and when d [u] + G [u] [ v] == d [v], and c [u] + cost [u] [v]> [v] the num [u] was added num [v], the code is as follows:

for(int v = 0; v < n; v++){
	if(vis[v] == false && G[u][v] != INF){
		if(d[u] + G[u][v] < d[v]){
			d[v] = d[u] + G[u][v];
			num[v] = num[u];
		}else if(d[u] + G[u][v] == d[v){
			num[v] += num[u];
		}
	}
} 

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/93230765