Dijkstra template for Shortest Path
I. Time Complexity: Dijkstra of O (V * V)
Const int Max_v = 100 + 10; const int INF = 1 <30; int cost [Max_v] [Max_v]; // weight int d [Max_v]; // The shortest distance bool used [Max_v] from vertex s; // use the used graph int V; // Number of vertices int Edge; // Number of edges void dijkstra (int s) {fill (d, d + V, INF); fill (used, used + V, false); d [s] = 0; while (true) {int v =-1; for (int u = 0; u
< d[v])) v="u;" } if(v="=" -1) break; used[v]="true;" for(int u
Ii. Time Complexity: Dijkstra (priority queue optimized) of O (E * logV)
Const int maxn = 1000 + 10; const int INF = 1 <30; typedef pair
P; int N, R; struct edge {int to, cost;}; vector
G [maxn]; int d [maxn]; // Shortest Path void solve () {priority_queue
, Greater
> Que; fill (d, d + N, INF); // the Shortest Path d [0] = 0; que. push (P (0, 0); while (! Que. empty () {P p = que. top (); que. pop (); int v = p. second; if (d [v] <p. first) continue; for (int I = 0; I
D [v] + e. cost) {d [e. to] = d [v] + e. cost; que. push (P (d [e. to], e. to); }}printf ("% d \ n", d [N-1]);}
Iii. template question HDU2544
Description
In each year's competition, all the finalists will get a very beautiful t-shirt. However, every time our staff moved hundreds of pieces of clothing from the store back to the stadium, they were very tired! So now they want to find the shortest route from the store to the stadium. Can you help them?
Input
The input includes multiple groups of data. The first row of each group of data is two integers, N and M (N <= 100, M <= 10000). N indicates several intersections on the streets of Chengdu, the intersection marked as 1 is the location of the store, the intersection marked as N is the location of the stadium, and M represents several roads in Chengdu. N = M = 0 indicates that the input is complete. In the next M row, each row contains three integers, A, B, and C (1 <= A, B <= N, 1 <= C <= 1000 ), it means there is A road between Intersection A and intersection B. Our staff need to walk this road in C minutes.
Enter a route to ensure there is at least one store.
Output
Output a line for each group of inputs, indicating the shortest time for a staff member to walk from the store to the stadium
Sample Input
2 11 2 33 31 2 52 3 53 1 20 0
Sample Output
32
First AC code
# Include
# Include
# Include
# Include
# Include
# Include using namespace std; const int Max_v = 100 + 10; const int INF = 1 <30; int cost [Max_v] [Max_v]; // weight int d [Max_v]; // The shortest bool used [Max_v] Starting from vertex s; // use the used graph int V; // The number of vertices int Edge; // Number of edges void dijkstra (int s) {fill (d, d + V, INF); fill (used, used + V, false); d [s] = 0; while (true) {int v =-1; for (int u = 0; u
< d[v])) v="u;" } if(v="=" -1) break; used[v]="true;" for(int u
Type 2 AC code (heap optimization)
# Include
# Include
# Include
# Include
# Include using namespace std; const int maxn = 1000 + 10; const int INF = 1 <30; typedef pair
P; int N, R; struct edge {int to, cost;}; vector
G [maxn]; int d [maxn]; // Shortest Path void solve () {priority_queue
, Greater
> Que; fill (d, d + N, INF); // the Shortest Path d [0] = 0; que. push (P (0, 0); while (! Que. empty () {P p = que. top (); que. pop (); int v = p. second; if (d [v] <p. first) continue; for (int I = 0; I
D [v] + e. cost) {d [e. to] = d [v] + e. cost; que. push (P (d [e. to], e. to); }}printf ("% d \ n", d [N-1]);} int main () {int I, a, B, c; edge e; while (~ Scanf ("% d", & N, & R) {if (N = 0 & R = 0) break; for (I = 0; I <maxn; I ++) G [I]. clear (); for (I = 0; I