CF786B 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:
- With a plan of this type you can open a portal from planet v to planet u.
- With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
- 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.
The first line of input contains three integers n, q and s (1 ≤ n, q ≤ 105, 1 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.
The next q lines contain the plans. Each line starts with a number t, type of that plan (1 ≤ t ≤ 3). If t = 1 then it is followed by three integers v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r and w where w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).
In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or - 1 if it's impossible to get to that planet.
3 5 1
2 3 2 3 17
2 3 2 2 16
2 2 2 3 3
3 3 1 1 12
1 3 3 17
0 28 12
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
0 -1 -1 12
In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.
二、思路:
很明显这是一道线段树优化建图的板子题。
首先先来官方题解:
786B - Legacy
Consider a weighted directed graph (initially it has n vertices and no edges). We will construct a segment tree to handle queries of second type (and one for the third type but with similar approach).
Build a segment tree on number 1, ..., n. For each node of segment tree consider a vertex in the graph. For each leaf in this tree (like one with interval [l, l + 1)), add an edge with weight equal to 0 from vertex corresponding to this node to vertex l in the original graph. And for each non-leaf node, add an edge with weight equal to 0 from vertex corresponding to this node to the vertex corresponding to node of each of its children.
So we're adding about 4n vertices and edges to the graph. For each query of second type, we will add an edge from v to each maximal node of segment tree that [l, r) contains (lg(n) nodes for each query) with weight equal to w.
And construct a segment tree in the same way for queries of third type. Finally run Dijkstra's algorithm on this graph.
Time complexity:
看完官方题解,我们能有一个大致的思路。线段树的作用,就是将一些很密集的边通过一些虚点进行整理,从而能达到大大减少边数的目的。
具体来说是这样的。
建立两颗线段树\(T_0,T_1\),其叶子结点表示原图上的点。\(T_0\)上的所有节点向其父亲连边,\(T_1\)上的所有节点向其儿子连边。对于\(T_1\)和\(T_2\)的所有叶子结点,对应着连双向边。
对于操作1,我们只需在\(T_0\)上的\(v\)向\(T_1\)上的\(u\)连边即可。
对于操作2,我们需要在\(T_1\)上将区间\([l,r]\)划分成\(\log len\)段。从\(T_0\)上的\(v\)向这\(\log len\)段的节点各连一条边。
对于操作3,我们需要在\(T_0\)上将区间\([l,r]\)划分成\(\log len\)段。把这\(\log len\)段的节点向\(T_1\)上的\(v\)连边。
最终,我们从\(T_0\)上的\(s\)节点跑 Dijkstra 算法,在\(T_1\)上的叶子结点查询答案即可。
三、代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
inline int read(void) {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return f * x;
}
const int maxn = 1e5 + 3, maxv = maxn * 8, maxe = maxv * 2 + maxn * 18;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int n, Q, s, head[maxv], tot;
long long d[maxv];
bool vis[maxv];
struct Edge {
int y, next;
long long w;
Edge() {}
Edge(int _y, int _next, long long _w) : y(_y), next(_next), w(_w) {}
}e[maxe];
inline void connect(int x, int y, long long w) {
e[++ tot] = Edge(y, head[x], w);
head[x] = tot;
}
inline int get(int x, int k) {
if (!k) return x;
return x + 4 * n;
}
struct SegmentTree {
#define lson (o << 1)
#define rson (o << 1 | 1)
int L[maxn << 2], R[maxn << 2];
inline void build(int o, int l, int r, bool k) {
L[o] = l; R[o] = r;
if (l == r) {
connect(get(o, k), get(o, k ^ 1), 0);
return;
}
int mid = (L[o] + R[o]) >> 1;
build(lson, l, mid, k); build(rson, mid + 1, r, k);
if (!k) {
connect(get(lson, k), get(o, k), 0); connect(get(rson, k), get(o, k), 0);
}
else {
connect(get(o, k), get(lson, k), 0); connect(get(o, k), get(rson, k), 0);
}
}
int find(int o, int q, int k) {
if (L[o] == R[o]) return get(o, k);
int mid = (L[o] + R[o]) >> 1;
if (q <= mid) return find(lson, q, k);
return find(rson, q, k);
}
void add(int o, int ql, int qr, int to, long long w, bool k) {
// k == 0: [ql, qr] -> to
// k == 1: to -> [ql, qr]
if (ql <= L[o] && R[o] <= qr) {
if (!k) connect(get(o, k), to, w);
else connect(to, get(o, k), w);
return;
}
int mid = (L[o] + R[o]) >> 1;
if (ql <= mid) add(lson, ql, qr, to, w, k);
if (qr > mid) add(rson, ql, qr, to, w, k);
}
void print(int o, bool k) {
if (L[o] == R[o]) {
printf("%lld ", d[get(o, k)] != inf ? d[get(o, k)] : -1);
return;
}
print(lson, k); print(rson, k);
}
}T[2];
priority_queue<pair<long long, int> >q;
inline void Dijkstra(int S) {
q.push(make_pair(0, S));
memset(d, 0x3f, sizeof d);
d[S] = 0;
while (q.size()) {
int x = q.top().second; q.pop();
if (vis[x]) continue;
vis[x] = true;
for (int i = head[x]; i; i = e[i].next) {
int y = e[i].y;
if (d[y] > d[x] + e[i].w) {
d[y] = d[x] + e[i].w;
q.push(make_pair(-d[y], y));
}
}
}
}
int main() {
n = read(); Q = read(); s = read();
T[0].build(1, 1, n, 0);
T[1].build(1, 1, n, 1);
for (int i = 1; i <= Q; ++ i) {
int opt = read();
if (opt == 1) {
int u = read(), v = read(), w = read();
int x = T[0].find(1, u, 0);
T[1].add(1, v, v, x, w, 1);
}
else if (opt == 2) {
int u = read(), l = read(), r = read(), w = read();
int x = T[0].find(1, u, 0);
T[1].add(1, l, r, x, w, 1);
}
else if (opt == 3) {
int u = read(), l = read(), r = read(), w = read();
int y = T[1].find(1, u, 1);
T[0].add(1, l, r, y, w, 0);
}
}
Dijkstra(T[0].find(1, s, 0));
T[1].print(1, 1);
puts("");
return 0;
}