DijkstraAlgorithm: Directed graph G = (V, E)
1. Put the Source Vertex V0 into the set S, and the counter C = 0. Each node has a structure data D (A, B). A indicates the shortest path length to v0, and B indicates the frontend node of. Initialize V0 to D (0,-) and all nodes except V0 are D (INF ,-). INF indicates infinity.
2. Update all node u in non-set S by traversing all node v in set S.
The update condition is d [u]. first = min {du [u]. first, d [v]. first + wt (v, u)}, where D [u]. first indicates each element of the data of the U node, that is, the shortest path length from the u node to the source node. Wt (v, u) indicates the path length from node v to node u. In addition to the shortest path length, the node is also updated.
In this case, the data of all nodes not in set S is updated. If
A. The data D of all nodes in the non-set S is (INF,-). The figure below shows that the nodes in the non-set S are not reachable from the source point.
B. Otherwise, select the node with the least number of D. First from this node and add it to the set S. The counter is incremented by one.
3. When the Counter C = n-1, the figure shows the desired result and all nodes are reachable.
Optimization: when updating all nodes in non-set S in step 1, you do not need to traverse all nodes in set S. You only need to judge the newly added nodes in the last iteration.
Question: How can the explorer obtain the things required by the chief with the least gold coins. Each node has a value of grade and gold coins. If item A can be replaced by item B and item C, the edge weight from item A to item B is C.
Analysis: each item is a node. The item required by the chief is the source node. Find the shortest path from the source node to other nodes. The result is to add the item value and minimum value to the shortest path of all nodes. Notes:
1. Level processing: items with a level greater than m cannot be exchanged, including indirect exchanges. For example, the levels A, B, and C are 1, 2, and 3. If the level is limited to 1, you cannot change A to B or B to C, because the level of A and C exceeds 1. Therefore, if Dijkstra finds the shortest path at a time, it is difficult to determine whether the node is added to a non-set S, because it is unclear whether the current node meets the level requirements, or whether it will affect the number of nodes to be added in the future. So we are currently using the enumeration range method. One thing we are sure of is that the level of the initial node must be within the level range. Therefore, we can enumerate (lev_[ S]-M, lev_[ S]) to (lev_[ s], lev_[ S] + M) levels. When the shortest path is added to a node, the node level must be greater than or equal to the lowest level and less than or equal to the highest level.
2. Directed Graph problem: item A can be replaced by item B and item C, indicating that the edge right between item A and item B is C, but it does not mean that B to a also has an edge with the right to C. Note!
3. Initialization problem: Since Dijkstra needs to be run multiple times, the minimum path length must be initialized. My previous wa was wrong here because the dijstra data is similar each time, so a lot of data cannot be checked out.
4. the INF = (1> 30)-1 representing infinity is sufficient, and the int used for the adjacent matrix is also sufficient, not like some people mentioned in discuss who want to open INF = (1> 31)-1.
The source code is as follows:
# Include <iostream> <br/> # include <cstring> <br/> using namespace STD; </P> <p> const int n = 128; <br/> const int INF = (1 <30)-1; </P> <p> int adj [N] [N]; <br/> int lev[ N]; <br/> int Val [N]; <br/> int V [N]; </P> <p> int m; // level gap <br/> int littlelev and biglev; // the lowest and highest levels of Dijkstra </P> <p>/* s indicates the source point, n is the number of vertices */<br/> int Dijkstra (int s, int N) {<br/> bool is [N]; <br/> int pre = s; </P> <p> memset (is, false, sizeof (is); <br/> (Int I = 0; I <n; I ++) <br/> V [I] = inf; <br/> is [s] = true, V [s] = 0; <br/> for (Int J = 0; j <n-1; j ++) {<br/> int min = inf, ID; <br/> for (INT I = 0; I <n; I ++) if (! Is [I]) <br/> If (column [I]> = littlelev & Column [I] <= biglevev) // remove this level constraint as the standard single-source shortest path <br/>{< br/> If (V [I]> V [pre] + adj [pre] [I ]) <br/> V [I] = V [pre] + adj [pre] [I]; <br/> If (min> V [I]) <br/> min = V [ID = I]; <br/>}< br/> If (Min! = Inf) // Add a new vertex <br/> pre = ID, is [ID] = true; <br/> else // indicates that some vertices are inaccessible <br/> break; <br/>}< br/> int min = inf; <br/> for (INT I = 0; I <n; I ++) if (V [I]! = Inf) // you need to add the lowest item price when seeking the Shortest Path <br/> If (min> V [I] + val [I]) <br/> min = V [I] + val [I]; <br/> return min; <br/>}</P> <p> int main () <br/>{< br/> int N; </P> <p> CIN> m> N; <br/> for (INT I = 0; I <n; I ++) <br/> for (Int J = 0; j <n; j ++) <br/> adj [I] [J] = (I = J )? 0: INF; // except the corner edge, it should be an infinitely large value <br/> for (INT I = 0; I <n; I ++) {<br/> int C, DIS, ID; <br/> CIN> Val [I]> lev[ I]> C; <br/> for (Int J = 0; j <C; j ++) {<br/> CIN> ID> DIS; <br/> adj [I] [ID-1] = DIS; // directed graph <br/>}< br/> int min = inf, ans; <br/> for (INT I = lie [0]-m; I <= lie [0]; I ++) {// enumerate all levels <br/> littlelev = I; <br/> biglev= I + m; <br/> ans = Dijkstra (0, N ); <br/> If (min> ans) <br/> min = ans; <br/>}< br/> cout <min <Endl; </P> <p> return 0; <br/>}< br/>