Legacy

题目描述

Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.

There are n planets in their universe numbered from $1$ to $n$. Rick is in planet number $s$ (the earth) and he doesn’t know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he’s still using its free trial.

By default he can not open any portal by this gun. There are $q$ plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.

Plans on the website have three types:

  1. With a plan of this type you can open a portal from planet $v$ to planet $u$.
  2. With a plan of this type you can open a portal from planet $v$ to any planet with index in range $[l, r]$.
  3. With a plan of this type you can open a portal from any planet with index in range $[l, r]$ to planet $v$.

Rick doesn’t known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.

题意概述

有$n$个点以及$q$条边。边有$3$种类型:① 从点$v$到点$u$;② 从点$v$到编号在$[l, r]$之间的点;③ 从编号在$[l, r]$之间的点到点$v$。每条边都有一个权值$w$。求从点$s$出发到其他所有点的最短路径长度。

数据范围:$1 \le n, q \le 10^5, \ 1 \le w \le 10^9$。

算法分析

分别从上到下、从下到上建立线段树,但边的方向均为从上到下,边权均为$0$。

如图,红色边表示线段树中的边。对于输入的每一条边,在这张图中建立相对应的边:① 直接在最中间一层连边;② 从最中间一层的点向上面的线段树连边;③ 从下面的线段树向最中间一层的点连边。最后跑一遍最短路即可。显然,点的数目为$O(n)$,边的数目为$O(n\log n)$,因此可以 AC。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
struct node_type {
long long l, r, child[2];
} node[2000001];
struct edge {
long long v, w, nxt;
} e[2000001];
long long n, q, s, dist[2000001], tot, nume, h[2000001];
bool in[2000001];
queue<long long> que;
void add_edge(long long u, long long v, long long w) {
if (u == v) return;
e[++nume].v = v, e[nume].w = w, e[nume].nxt = h[u], h[u] = nume;
}
void build_tree(long long root, int mode) {
long long mid = node[root].l + node[root].r >> 1;
if (node[root].l == mid) node[root].child[0] = node[root].l;
else {
node[++tot].l = node[root].l, node[tot].r = mid;
node[root].child[0] = tot, build_tree(tot, mode);
}
if (mid + 1 == node[root].r) node[root].child[1] = node[root].r;
else {
node[++tot].l = mid + 1, node[tot].r = node[root].r;
node[root].child[1] = tot, build_tree(tot, mode);
}
if (mode) {
add_edge(node[root].child[0], root, 0);
add_edge(node[root].child[1], root, 0);
} else {
add_edge(root, node[root].child[0], 0);
add_edge(root, node[root].child[1], 0);
}
}
void insert_line(long long root, long long l, long long r, long long v, long long w, int mode) {
if (l == node[root].l && r == node[root].r) {
if (!mode) add_edge(v, root, w);
else add_edge(root, v, w);
return;
}
if (r < node[node[root].child[1]].l) insert_line(node[root].child[0], l, r, v, w, mode);
else if (l > node[node[root].child[0]].r) insert_line(node[root].child[1], l, r, v, w, mode);
else {
insert_line(node[root].child[0], l, node[node[root].child[0]].r, v, w, mode);
insert_line(node[root].child[1], node[node[root].child[1]].l, r, v, w, mode);
}
}
void spfa() {
memset(dist, 0x1f, sizeof(dist));
while (!que.empty()) que.pop();
que.push(s), in[s] = true, dist[s] = 0;
while (!que.empty()) {
long long t = que.front(); que.pop(); in[t] = false;
for (long long i = h[t]; i; i = e[i].nxt) {
if (dist[e[i].v] > dist[t] + e[i].w) {
dist[e[i].v] = dist[t] + e[i].w;
if (!in[e[i].v]) que.push(e[i].v), in[e[i].v] = true;
}
}
}
}
int main()
{
scanf("%lld%lld%lld", &n, &q, &s);
for (int i = 1; i <= n; ++i) {
node[i].l = node[i].r = i;
}
tot = n + 2;
node[n + 1].l = node[n + 2].l = 1;
node[n + 1].r = node[n + 2].r = n;
if (n > 1) build_tree(n + 1, 0), build_tree(n + 2, 1);
for (long long i = 1, t, v, u, l, r, w; i <= q; ++i) {
scanf("%lld", &t);
if (t == 1) {
scanf("%lld%lld%lld", &v, &u, &w);
add_edge(v, u, w);
} else {
scanf("%lld%lld%lld%lld", &v, &l, &r, &w);
insert_line(n + t - 1, l, r, v, w, t - 2);
}
}
spfa();
for (int i = 1; i <= n; ++i) {
if (dist[i] != dist[0]) printf("%lld ", dist[i]);
else printf("-1 ");
}
printf("\n");
return 0;
}

Legacy
https://regmsif.cf/2017/07/05/oi/legacy/
作者
RegMs If
发布于
2017年7月5日
许可协议