Dijkstra algorithm
Before you come to a maze. The maze consists of a number of rooms, each with a score, and the first time you enter this room, you can get this score. There are several bidirectional roads linking these rooms, and it takes some time for you to walk from one room to another in these roads. The game prescribes your starting and ending rooms, your first goal is to reach the end point as soon as possible from the beginning, and in the premise of satisfying the primary goal, make your score as large as possible. Now that's the problem, given the room, the road, the score, the starting point, and the end point, can you calculate the maximum score for you to leave the maze as soon as possible?
Dijkstra algorithm is a classic algorithm-he is a Dutch computer scientist Dijkstra in 1959, the single source map shortest path algorithm, is also a classic greedy algorithm. The so-called Tanyuanto is a graph that specifies a starting point, and our shortest path is calculated from this point of departure. The application of the algorithm is a non-direction (or a graph), all edge rights are non-negative.
Algorithm Description:
Node Set v = {} empty collection, distance initialized.
Node Number 0: n–1, start number 0≤s < n.
Distance array
Starting point D[s] = 0
Other d[i] =∞, 0≤i < N, I≠s. Loop n times
Node I found that node i is not of V and D[i] has the lowest value.
v = v + i
For all edges satisfying J V (i, j) Update d[j] = min (D[j], D[i] + W (i, J)). For example, describe the running process of the Dijkstra algorithm:
Initially, the shortest path (also called single Source shortest path) to other points is obtained. Initialize a point a point has 3 edges, AB (+), AE (+), AF (1). Add 3 edges to the priority queue, at which point the elements in the queue are (only the target points are recorded):
{1 F} | {E} | {B} takes the smallest element in the queue, the {1 f},f point is an unhandled point, so the shortest distance from point A to F points is obtained. Update distance, change to: Process F point, F Point has 4 edges. FA (1), FB (one), FD (+), FE (33). Where FA has been processed, so ignore. Add 3 edges to the priority queue, note that when you join a queue at this point, the weights of all sides need to be added to the minimum distance from F to point 1. The elements in this queue are:
{B} | {D} | {E} | {B} | {E} takes out the smallest element in the queue, {The b},b point is an unhandled point, thus obtaining the shortest distance from point A to point B. Update distance, change to: Handle B point, B Point has 4 sides. AB (+), BF (one), BC (6), BD (5). Where AB,BF has been processed, so ignore.
Add the priority queue by adding the weight of the 2 bar to the shortest path 12 of a to B. The elements in this queue are:
{D} | {E} | {B} | {D} | {C} | {E}
Remove the smallest element in the queue, {The d},d point is an unhandled point, so the shortest distance from point A to D is obtained. Update distance, change to:
Handle d point with 4 sides of D point. where DC (Ten), DE (4) has not been processed.
Add the weight of the 2 bar to the shortest path of a through D to 15 and join the priority queue. The elements in this queue are:
{E} | {B} | {D} | {C} | {E} | {C} | {E}
Remove the smallest element in the queue, {The e},e point is an unhandled point, so the shortest distance from point A to E is obtained. Update distance, change to:
The e point is processed and the edges connected to the e point have been processed.
At this point the elements in the priority queue are:
{B} | {D} | {C} | {E} | {C} | {E}
Remove the smallest element in the queue, {The b},b point is a point that has already been processed, so continue with the subsequent processing. {D} | {C} | {E} | {C} | {E} takes out the smallest element in the queue, {The d},d point is a point that has already been processed, so continue with the subsequent processing. {C} | {E} | {C} | {E}
Take out the smallest element in the queue, {The c},c point is an unhandled point, so you get the shortest distance from point A to point C. Update distance, change to:
Proof of the Dijkstra algorithm:
I V, d[i] = Min{d[x] + w (x, I), X v}
When we prove that node I is going into set V, D[i] is indeed the shortest path length of s to I.
Inductive proof: At first d[s] = 0 satisfies the condition. Assuming that the points in the previous set V all satisfy the assumptions, now join the node I v, assuming any path from S to i p= s...x y...i.
where S.. X is all in V, Y v. According to the inductive hypothesis d[x] is the shortest short length of s to X. According to the definition of D, we have d[x] + w (x, y) ≥d[y].
And because Dijkstra chooses the smallest d to join, so there is d[y]≥d[i].
So there is the length of the path P, and Length (p) ≥d[x] + w (x, y) + length (y). i) ≥d[y] + length (y). i) ≥d[y]≥d[i].
Thus D[i] is also the shortest short length. Evidence. Finally, we provide input and output data, you write a program, the implementation of this algorithm, only write the correct program, to continue the course behind.
input
The first line is 4 integers n (<=500), M, start, end. n represents the number of rooms, the room number from 0 to (n-1), m for the number of roads, any two rooms with a maximum of one road, start and end represents the start and end of the room number. The second line contains n spaces separated by a positive integer (no more than 600), which represents your score into each room. Next m line, each line of 3 spaces separated by the integer x, y, Z (0<z<=200) represents the road, representing the road from room X to room y (bidirectional), note that at most only one road is connected to two rooms, you need time for Z. The input guarantees that there is at least one path from start to end.
Output
One line, two space-delimited integers, the first representing the minimum amount of time you need, and the second representing the maximum score you can get on the minimum time premise.
Input Example
3 2 0 21 2 30 1 101 2 11
Sample Output
21 6
The big guy's code.
#include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #define INF 0x3f3f3f3fusing namespace Std;const int MAX = 550;int Co[max], Dist[max], G[max][max],low[max];int N, M, s, E;bool Vis[MAX ];void Dijistra () {for (int i = 0; i < n; i + +) {Dist[i] = INF; } memset (Vis,0,sizeof (VIS)); memset (low,0,sizeof (VIS)); Dist[s] = 0; Low[s] = Co[s]; for (int i = 1; I <= n; i + +) {int mins = INF, MAx = 0, pos; for (int j = 0; J < N; j + +) {if (!vis[j] && dist[j] < mins) {pos = j; mins = Dist[j]; MAx = Co[j]; } if (!vis[j] && dist[j] = = mins && MAx < low[j]) {pos = j; MAx = Low[j]; }} if (mins = = INF) break; Vis[pos] = true; for (int j = 1; J <= N; j + +) {if (!vis[j] && dist[j] > Dist[pos] + g[pos][j]) {DIST[J] = Dist[pos] + g[pos][j]; LOW[J] = Low[pos] + co[j]; } if (!vis[j] && low[j] < Low[pos] + co[j] && dist[j] = = Dist[pos]+g[pos][j]) { LOW[J] = Low[pos] + co[j]; }}}}int Main () {scanf ("%d%d%d%d", &n,&m,&s,&e); for (int i = 0; i < n; i + +) {for (int j = 0; J < N; j + +) G[i][j] = (i==j)? 0:inf; } for (int i = 0; i < n; i + +) scanf ("%d", &co[i]); for (int i = 0; i < m; i + +) {int u, V, W; scanf ("%d%d%d", &u,&v,&w); if (G[u][v] > W) {g[u][v] = g[v][u] = w; }} Dijistra (); printf ("%d%d\n", dist[e],low[e]); return 0;}
51nod-Maze problem (Dijkstra algorithm)