#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:102400000,102400000")
#include <queue>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 200010;
typedef pair<ll, int> P;
int L[maxn], R[maxn], f[maxn], n;
ll dis[maxn], C[maxn];
int find1(int x) {
if (x != f[x]) f[x] = find1(f[x]);
return f[x];
}
void unite(int a, int b) {
int aa = find1(a);
int bb = find1(b);
if (aa == bb) return;
f[aa] = bb;
}
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P> >que;
memset(dis, INF, sizeof(dis));
dis[s] = C[s];
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
for (int i = -1; i <= 1; i += 2) {
int le = v + i*L[v];
int ri = v + i*R[v];
if (le>ri) swap(le, ri);
le = max(1, le);
ri = min(ri, n);
if (le>ri) continue;
int ff = le;
while (1) {
ff = find1(ff);
if (ff>ri) break;
if (dis[ff]>dis[v] + C[ff]) {
dis[ff] = dis[v] + C[ff];
que.push(P(dis[ff], ff));
}
unite(ff, ff + 1);
ff++;
}
}
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &L[i]);
for (int i = 1; i <= n; i++) scanf("%d", &R[i]);
for (int i = 1; i <= n; i++) scanf("%I64d", &C[i]);
for (int i = 1; i<n + 5; i++) f[i] = i;
dijkstra(1);
for (int i = 1; i <= n; i++) {
if (dis[i] != INF) {
if (i == n) printf("%I64d\n", dis[i] - C[i]);
else printf("%I64d ", dis[i] - C[i]);
}
else {
if (i == n) printf("-1\n");
else printf("-1 ");
}
}
}
return 0;
}
hdu5361 最短路+并查集
最新推荐文章于 2022-05-07 00:46:01 发布