Test instructions: GBN recently planned to cross a forest, but he was very proud, so he decided to go only a few special roads, he intended to go only along the road (A, b) to meet the following conditions: There is a road from the start of the home, the path is shorter than all from A home. Your task is to calculate how many different home paths there are. Where the starting point is numbered 1 and the end number is 2.
Idea: First from the end Dijkstra once, to find out each point u home of the shortest path length, then equivalent to create a new diagram, when D[b]<d[a] when there is a point A to B has an edge, then the goal is to find out the starting point to the end of the road number. Because this diagram is a DAG, it can be solved with dynamic programming.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = + 5;const int INF = 100000000;int n, m; Dijkstra struct Edge {int from, to, Dist; Edge (int u = 0, int v = 0, int d = 0): From (U), to (v), Dist (d) {}};struct Heapnode {///used to the node int d of the priority queue, U;bool operator < (const heapnode& RHS) Const {return d > rhs.d;}}; struct Dijkstra {int n, m; Points and the number of sides vector<edge> edges; Edge list vector<int> G[MAXN]; Number of edges starting at each node bool DONE[MAXN]; Whether it has been permanently numbered int D[MAXN]; s to various points of distance int P[MAXN]; The last edge in the shortest path ll dp[maxn];void init (int n) {this->n = n;for (int i = 0; i < n; i++) g[i].clear (); Edges.clear (); Memset (DP ,-1, sizeof (DP));} void Addedge (int from, int to, int dist) {//If the graph needs to be called two times Edges.push_back (Edge (from, to, dist); m = Edges.size (); G[from].push_back (m-1);} void Dijkstra (int s) {//for S to all points distance priority_queue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10917 Walk Through the Forest (dijkstra+dag dynamic planning)