hdu5361 (2015 + school 6)--in Touch (deformed Dijkstra)

Source: Internet
Author: User

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)

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.