There are n points and m undirected edges. There is an upper limit on the fuel tank. Each unit of gasoline can go 1 unit distance, and the oil price of each city is val [I]. For each query, calculate the minimum cost from s to e.
Dp [I] [j] indicates the minimum cost when the remaining fuel volume in the fuel tank reaches the I city. The time complexity is acceptable when bfs is used to expand nodes and each node is split into 100 nodes.
# Include <algorithm> # include <iostream> # include <cstring> # include <cstdlib> # include <fstream> # include <sstream> # include <bitset> # include <vector> # include <string> # include <cstdio> # include <cmath> # include <stack> # include <queue> # include <stack> # include <map> # include <set> # define FF (I, a, B) for (int I = a; I <B; I ++) # define FD (I, a, B) for (int I =; i> = B; I --) # define REP (I, n) for (int I = 0; I <n; I ++) # define CLR (a, B) mem Set (a, B, sizeof (a) # define debug puts ("** debug **") # define LL long # define PB push_backusing namespace std; const int maxn = 1010; int n, m, val [maxn], dp [maxn] [101]; int Q, C, S, E; struct Node {int u, cost, res; // current Node, cost, remaining fuel bool operator <(const Node rhs) const {return cost> rhs. cost ;}}; struct Edge {int to, dist ;}; vector <Edge> edges; vector <int> G [maxn]; inline void init () {REP (I, n) G [I]. cl Ear (); edges. clear ();} void add (int a, int B, int c) {edges. PB (Edge) {B, c}); edges. PB (Edge) {a, c}); int nc = edges. size (); G [a]. PB (nc-2); G [B]. PB (nc-1);} void bfs () {REP (I, n) REP (j, C + 1) dp [I] [j] = 0; priority_queue <Node> q; q. push (Node) {S, 0, 0}); while (! Q. empty () {Node x = q. top (); q. pop (); int u = x. u, cost = x. cost, res = x. res, nc = G [x. u]. size (); if (u = E) {printf ("% d \ n", cost); return;} // consider the current status, if (res <C & (dp [u] [res + 1] = 0 | dp [u] [res + 1]> cost + val [u]) dp [u] [res + 1] = cost + val [u], q. push (Node) {u, dp [u] [res + 1], res + 1}); REP (I, nc) {int v = edges [G [u] [I]. to, dist = edges [G [u] [I]. dist; if (res <dist) continue; // if the oil volume is insufficient, go to the next section. Point: continue. // if the current fuel volume is used to the next node, it will be better if (dp [v] [res-dist] = 0 | dp [v] [res-dist ]> cost) dp [v] [res-dist] = cost, q. push (Node) {v, cost, res-dist}) ;}} puts ("impossible"); return ;} int main () {while (~ Scanf ("% d", & n, & m) {init (); REP (I, n) scanf ("% d ", & val [I]); int a, B, c; REP (I, m) {scanf ("% d", & a, & B, & c); add (a, B, c);} scanf ("% d", & Q); while (Q --) {scanf ("% d", & C, & S, & E); bfs () ;}} return 0 ;}