HDU 5361 in Touch (2015 + School 6 1009)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5361

Test instructions: Shortest way to find the shortest distance from the source point to all points. But unlike the normal shortest circuit, the given edge is the distance from any point in the interval [l,r].
Enter an n, represent n points, enter n l[i], enter n r[i], enter n c[i].
For I, the distance from any point in the I to interval [i-r[i]],i-l[i]] and interval [i + L[i],i + r[i]] is c[i].
Find the shortest distance from 1 to each point.

Train of thought: Jo Jian side runs the shortest way, because side too much, so not feasible. Because in the final result, Energy point 1 to a certain interval of the shortest path is the same, so you can use the shortest path algorithm based on the line segment tree interval update to maintain the source point 1 to the shortest route of the interval, and finally only need to query the interval. The maximum and minimum value of the shortest path from the interval source point to the interval point in each node of the segment tree. So the leaf node is the shortest path from the source point 1 to that point.

Code:

#include <iostream>#include <iomanip>#include <stdio.h>#include <string>#include <string.h>#include <math.h>#include <queue>#include <set>#include <vector>#include <algorithm>using namespace STD;#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1Const Long LongINF =1e18;Const intN =2e5+Ten;structSegment {intL, R;Long LongD Segment (intL =0,intR =0,Long LongD =0) { ThisL = l; ThisR = r; ThisD = D; }friend BOOL operator< (Segment A, Segment b) {returnA.D > B.D; }};structNode {intUsed;Long Long_max;Long Long_min;}; Node Node[n <<2];Long LongLazy[n <<2];intNintLef[n];intRig[n];Long LongC[n];p riority_queue<segment> q;voidPushup (intRT) {Node[rt]._max = max (Node[rt <<1]._max, Node[rt <<1|1]._max); node[rt]._min = min (node[rt <<1]._min, Node[rt <<1|1]._min);if(Node[rt <<1].used = = Node[rt <<1|1].used) node[rt].used = Node[rt <<1].used;Elsenode[rt].used =-1;}voidPushdown (intRT) {if(LAZY[RT]! =-1) {Lazy[rt <<1] = Lazy[rt <<1|1] = Lazy[rt]; Node[rt <<1]._min = Node[rt <<1]._max = Lazy[rt]; Node[rt <<1|1]._min = Node[rt <<1|1]._max = Lazy[rt]; LAZY[RT] =-1; }}voidBuildintLintRintRT) {Node[rt]._max = INF;    Node[rt]._min = INF; node[rt].used =0; LAZY[RT] =-1;if(L = = r) {if(L = =1) {Node[rt]._max =0; Node[rt]._min =0; }return; }intm = (L + r) >>1;    Build (Lson);    Build (Rson); Pushup (RT);}voidUpdateLong LongCrintLintRintLintRintRT) {if(Node[rt]._max <= CR)return;if(l <= L && R <= R) {Node[rt]._max = CR;if(Node[rt]._min > CR)            {node[rt]._min = CR;            Q.push (Segment (L, R, CR)); LAZY[RT] = CR;return; }    }if(L = = r)return; Pushdown (RT);intm = (L + r) >>1;if(l <= m) update (CR, L, R, Lson);if(r > M) Update (CR, L, R, Rson); Pushup (RT);}voidQuerysegment (Segment FF,intLintRintRT) {if(node[rt].used = =1)return;if(FF.L <= l && R <= FF.R) {if(node[rt].used = =0) { for(inti = l; I <= R; i++) {intLe = i + lef[i];intri = min (n, i + rig[i]);if(Le <= N) {Update (FF.D + c[i], le, RI,1N1); } le = max (1, I-rig[i]); RI = I-lef[i];if(RI >=1) {Update (FF.D + c[i], le, RI,1N1); }} node[rt].used =1;return; }    }if(L = = r)return;intm = (L + r) >>1;if(FF.L <= m) querysegment (FF, Lson);if(Ff.r > M) querysegment (FF, Rson); Pushup (RT);}BOOLFirvoidQueryintLintRintRT) {if(Node[rt]._max = = node[rt]._min) { for(inti = l; I <= R; i++) {if(Node[rt]._min = = INF) Node[rt]._min =-1;if(!fir) {printf("%lld", node[rt]._min); FIR =true; }Else                 printf("%lld", node[rt]._min); }return; }if(L = = r) {return; } pushdown (RT);intm = (L + r) >>1;    Query (Lson); Query (Rson);}intMain () {intT_case;scanf("%d", &t_case); for(intI_case =1; I_case <= t_case; i_case++) {scanf("%d", &n); for(inti =1; I <= N; i++)scanf("%d", &lef[i]); for(inti =1; I <= N; i++)scanf("%d", &rig[i]); for(inti =1; I <= N; i++)scanf("%lld", &c[i]); Build1N1); while(!q.empty ()) Q.pop (); Q.push (Segment (1,1,0)); while(!q.empty ())            {Segment ff = q.top ();            Q.pop (); Querysegment (FF,1N1); } fir =false; Query1N1);printf("\ n"); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5361 in Touch (2015 + School 6 1009)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.