Topic Link: Click to open the link
Title: Give a sequence of n number, labeled 1 to N, for the number of I, it can be moved to the distance I for [Li,ri] position, the cost is c[i], enter three lines, the first row l[i], the second row r[i], the third row c[i], now ask for the first number, It moves to the minimum cost of the first I position. (1<=i<=n)
This is a point can be moved to any point in a paragraph, and the same cost, so that it does not apply to the existing four shortest way, but can be deformed Dijkstra, Dij is to find a minimum distance node, with this node to update other nodes, we can use a priority queue, Each time a point is found, use this point to update the other points, the point to meet Dis[i]+c[i] is the smallest, then the point of the update is the smallest point.
There is a priority queue to maintain the value of the Dis+c, using set to find a point in which there is no update, so that it can prevent traversing to the point that has been updated, resulting in a timeout.
#include <cstdio> #include <cstring> #include <queue> #include <set> #include <algorithm> using namespace std; #define LL __int64#pragma COMMENT (linker, "/stack:102400000,102400000") struct node{int id; LL C; BOOL operator < (Node A) const {return c > a.c; }} p, q;p riority_queue<node> que;set<int> s;set<int>:: iterator iter; LL dis[200010], c[200010]; int l[200010], r[200010]; void f (int l,int r) {iter = S.lower_bound (l); while (iter! = S.end ()) {if (*iter > R) break; dis[*iter] = p.c; Q.id = *iter; Q.C = p.c + c[q.id]; Que.push (q); iter++; S.erase (q.id); } return; int main () {int T, n, I, x, y; Freopen ("1009.in", "R", stdin); Freopen ("111.out", "w", stdout); scanf ("%d", &t); while (t--) {while (!que.empty ()) Que.pop (); memset (dis,-1,sizeof (dis)); S.clear (); scanf ("%D ", &n); for (i = 2; I <= n; i++) S.insert (i); for (i = 1; I <= n; i++) scanf ("%d", &l[i]); for (i = 1; I <= n; i++) scanf ("%d", &r[i]); for (i = 1; I <= n; i++) scanf ("%i64d", &c[i]); Dis[1] = 0; P.id = 1; P.C = c[1]; Que.push (P); while (!que.empty () &&!s.empty ()) {p = que.top (); Que.pop (); x = max (p.id-r[p.id],1); y = p.id-l[p.id]; if (y >= 1) f (x, y); x = p.id + l[p.id]; y = min (p.id + r[p.id],n); if (<= N) f (x, y); } for (i = 1; i < n; i++) printf ("%i64d", Dis[i]); printf ("%i64d\n", Dis[i]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu5361 (2015 + school 6)--in Touch (deformed Dijkstra)