jumping 反向加边/dijkstra堆优化

来源:互联网 发布:网络作家富豪榜2011 编辑:程序博客网 时间:2024/05/21 12:42

Shopping with the wife is one of men’s hardest marriage responsibilities. Nour is a good husband. So he goes out with his wife every Friday to “Damasquino” mall.

Damasquino is a very famous mall in Damascus. It has a very unique transportation method between shops. Since the shops in the mall are laying in a straight line, you can jump on a very advanced trampoline from the shop i, and land in shop (i + di) or (i - di) depending on the direction of the jump. Where di is a constant given for each shop. You can jump forward or backward only, and you can’t land any where before the first shop or after the last shop in the mall.

There are N shops in the mall, numbered from 1 to N. Nour’s wife starts her shopping journey from shop 1 and ends it in shop N. Unfortunately, Nour sometimes gets lost from his wife (the mall is very crowded on Fridays). So he wants to know for each shop, what is the minimum number of trampoline jumps he has to make in order to reach shop N and see his wife again!

Input
The first line consists of one integer T, the number of test cases.

Each test case consists of two lines, the first line contains a single integer (2 ≤ N ≤ 105) the number of shops in the mall. The second line contains N integers. Where the ith integer (1 ≤ di ≤ N) is the constant described above for the ith shop.

Output
For each test case, print N lines. The ith line should contain one integer, the minimum number of jumps needed to be made in order to reach shop N starting from shop i, or  - 1 if it is impossible.

Example
input
2
5
1 1 1 1 1
5
2 2 2 2 2
output
4
3
2
1
0
2
-1
1
-1
0

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#define MST(s,q) memset(s,q,sizeof(s))#define INF 0x3f3f3f3f#define MAXN 100005#define Lchild id<<1#define Rchild (id<<1)+1inline int lowbit(int x) {return x & (-x);}using namespace std;struct edge{    int u,  v, next;    int dis;    edge() {}    edge(int a, int b, int c) {u = a, v = b, dis = c;}    bool operator <(const edge a)const {        return dis > a.dis;    }} Edge[MAXN * 2], tmp;int dis[MAXN], vis[MAXN], Head[MAXN];void dijkstra(int N) {    memset(vis, 0, sizeof(vis));    memset(dis, INF, sizeof(dis));    dis[N] = 0;    priority_queue<edge> Q;    Q.push(edge(N, 0, 0));    while (!Q.empty()) {        tmp = Q.top();        Q.pop();        int u = tmp.u;        if (vis[u]) continue;        vis[u] = true;        for (int k = Head[u]; k != -1; k = Edge[k].next) {            int v = Edge[k].v;            if (!vis[v] && dis[v] > dis[u] + 1) {                dis[v] = dis[u] + 1;                Q.push(edge(v, 0, dis[v]));            }        }    }}int main() {     freopen("jumping.in", "r", stdin);    int T, N, a, b;    cin >> T;    while (T--) {        cin >> N;        int cnt = 0;        memset(Edge, 0, sizeof(Edge));        memset(Head, -1, sizeof(Head));        for (int i = 1; i <= N; i++)        {            scanf("%d", &a);            if (i - a > 0)  Edge[cnt].u = i - a, Edge[cnt].v = i , Edge[cnt].next = Head[i - a], Head[i - a] = cnt++;            if (i + a <= N) Edge[cnt].u = i + a, Edge[cnt].v = i , Edge[cnt].next = Head[i + a], Head[i + a] = cnt++;        }        dijkstra(N);        for (int i = 1; i <= N; i++)            printf("%d\n", dis[i] != INF ? dis[i] : (-1));    }}
0 0
原创粉丝点击