**/ * Solve single source Shortest path problem: The Dijkstra algorithm (the graph all sides of the weight of the non-negative) key (greedy): (1) Find the shortest distance has been determined by the node, from which to update its adjacent nodes of the minimum distance, (2) no longer care about (1) in the "shortest distance has been determined node." Time complexity (approximate analysis, inaccurate): "Find the node with the shortest distance identified" = O (| v|) " From it updates the shortest distance from its neighboring nodes "= + adjacency Matrix: O (| v|), adjacency table: O (| e|) Need to cycle above two steps V times, so time complexity: O (v^2) i.e.: in | e| smaller cases, time is mainly spent in finding the shortest distance the identified nodes are considered to be implemented with a binary heap (c + + priority_queue), and the operations that are put into binary heap data are up to | E| times, the average of the elements in the heap is about | v|, so time complexity O (| E|log| v|) -------------------------------------------- Basics:C + + priority_queue:Http://www.cplusplus.com/reference/queue/priority_queue/?kw=priority_queuethis context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top In the priority queue). By default, Priority_queue is done with a large root heap. **

from Baidu Encyclopedia: binary heap : Completely Two-dollar Tree (binary tree) or is approximately completely two-dollar Tree (binary tree). There are two types of binary heaps: the maximum heap and the smallest heap. : The key value of the parent node is always greater than or equal to the key value of any one of the child nodes; minimum heap (Keng Gen) : The key value of the parent node is always less than or equal to the key value of any one of the child nodes. full binary tree (complete binary tree) if the depth of the two-fork tree is H, except for the h layer, the other layers (1~h-1) The maximum number of nodes, the first h layer all the nodes are continuously concentrated on the leftmost, this is the complete binary tree. --------------------------------------------title: Poj 1860 currency exchange Use the maximum path to seek, Change the Dijkstra algorithm to determine the conditions can be. */

1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <cstddef>5#include <iterator>6#include <algorithm>7#include <string>8#include <locale>9#include <cmath>Ten#include <vector> One#include <cstring> A#include <map> -#include <utility> -#include <queue> the#include <stack> -#include <Set> -#include <functional> - using namespacestd; +typedef pair<Double,int> P;//First : Maximum currency value Second:the number of exchange points - Const intINF =0x3f3f3f3f; + Const intModprime =3046721; A Const DoubleEPS = 1e-9; at Const intMAXN = the; - - intN, M, S; - DoubleV; - DoubleD[MAXN]; - in structEdge - { to intto ; + DoubleRate ; - DoubleCommission; the }; * $Vector<edge>G[MAXN];Panax Notoginseng - voidSolve () the { +Fill (d, D + MAXN,-1); AD[s] =V; thePriority_queue<p, vector<p> >que; + Que.push (P (D[s], S)); - while(!que.empty ()) $ { $P p =que.top (); - Que.pop (); - intv =P.second; the if((v = = S) && (p.first-v) >EPS) - {Wuyiprintf"yes\n"); the return; - } Wu if(D[v] >P.first) - { About Continue; $ //if the currency value of the currently fetched node is not the currency maximum value of the node, discard the node currency value directly - /* - This situation occurs because: - constantly put updated data {p.first, p.second} into the binary heap, but it is possible to update the same node multiple times, A causes some nodes to be placed in the binary heap: the same node (P.second same), the node currency value is different (P.first different). + For example, suppose that the current binary heap is put in sequence {p.first1, p.second1},{p.first2, P.second1},{p.first3, the P.second1}, Max{p.first1, P.first2, p.first3} = P.first3, this time D[p.second] has been - update to P.FIRST3, when you remove {p.first1, P.second1},{p.first2, p.second1} from the two fork heap , $ Simply discard it, because {p.first3, p.second1} has been removed before the first two is removed, and {P.first3 has been used, the P.second1} updated the currency value of the other node (priority_queue:only the Max heap element can be the Retrieved (the one at the top and the priority queue). the */ the } - for(inti =0; I < g[v].size (); ++i) in { theEdge eg =G[v][i]; the if(D[eg.to] < (D[v]-eg.commission) *eg.rate) About { theD[eg.to] = (D[v]-eg.commission) *eg.rate; the Que.push (P (d[eg.to], eg.to)); the } + } - } theprintf"no\n");Bayi } the the intMain () - { - #ifdef HOME theFreopen ("inch","R", stdin); the //freopen ("Out", "w", stdout); the #endif the -scanf"%d %d%d%lf", &n, &m, &s, &V); the for(inti =0; i < M; ++i) the { the intCurrency1, Currency2;94scanf"%d%d", ¤cy1, ¤cy2); the Edge eg; theEg.to =Currency2; thescanf"%LF%LF", &eg.rate, &eg.commission);98 g[currency1].push_back (eg); About -Eg.to =Currency1;101scanf"%LF%LF", &eg.rate, &eg.commission);102 g[currency2].push_back (eg);103 }104 the Solve ();106 107 #ifdef HOME108Cerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl;109 _CrtDumpMemoryLeaks (); the #endif111 return 0; the}

Dijkstra algorithm (solving single source shortest path) detailed + POJ 1860 Currency Exchange