[CF464E]The Classic Problem

Description

给定一个 $n$ 点 $m$ 边的无向图,边权均以为 2 的 $k$ 次幂的形式给出,求 $S$ 到 $T$ 的最短路

$1 \le n, k \le 10 ^5, 1 \le m \le 3 \times 10 ^5$

Solution

首先还是必须用 Dijkstra 跑最短路的。

考虑中间复杂度爆炸的两种操作:

1
2
if (Chkmin(dis[v], dis[u] + w))
q.push(dis[v], v);

主要是加法和比较的复杂度问题。暴力高精无论时间还是空间显然爆炸

考虑给每个点维护一个叶子节点全是 01 的线段树来代表当前节点的 dis 值。

如何比较两个 dis 值的大小关系?

可以考虑把 01 序列像字符串一样找到 LCP(高位开始),比较 LCP 的最后一位就能知道他们之间的大小关系。

如何实现加法进位?

首先这是单点加法。如果这一位是 0 直接变 1,如果是 1 则找到从当前位开始极长的一段连续的 1,将他们区间置 0,再在后一位变 1 就可以了。

如何保证复杂度?

因为每个点的 dis 都是被某个点更新而来的,所以考虑主席树,区间置 0 的话直接把当前这个新树上那 $\log$ 段区间与其父亲断开连边,这样就不会影响历史版本状态了。也可以选择最开始建一棵全是 0 的线段树,直接把边连到这个线段树上就行。复杂度也是 $\log$,因为区间个数就这么多

每次开一个 tmp ,对这个 tmp 操作(单点置 1,区间置 0),然后如果 Chkmin 成功,直接 root[v] = tmp 即可。

比较和加法复杂度都是 $\cal O(\log k)$ 的(比较不需要空间,加法需要),然后 Dijkstra 过程需要 $\cal O((n + m) \log m)$ 次比较与加法,所以时空复杂度都是 $\cal O((n + m) \log m \log k)$ 的

代码参考了 hk 的,实现的很好,应当看看代码

Code

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;

typedef std::pair<int, int> pii;
typedef long long LL;

const int maxN = 1e5 + 2;
const int lim = 1e5 + 80;
const int mod = 1e9 + 7;

int n, m, k, id, S, T, ans;
int pw2[lim + 2];
vector<pii> G[maxN];
std::priority_queue<struct Info> q;
std::bitset<maxN> vis;

inline int Mod(int x)
{ return x >= mod ? x - mod : x < 0 ? x + mod : x; }

namespace SEG
{
int ncnt;
int root[maxN];
int lson[maxN << 7], rson[maxN << 7], val[maxN << 7];

// node first, arrange second, and else.

inline void Pushup(int cur, int l, int r)
{
int mid = (l + r) >> 1;
cur[val] = Mod(cur[lson][val] + (LL)cur[rson][val] * pw2[mid - l + 1] % mod);
}

void Insert(int& cur, int pre, int l, int r, int pos)
{
cur = ++ncnt;
if (l == r)
{
cur[val] = 1;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
cur[rson] = pre[rson], Insert(cur[lson], pre[lson], l, mid, pos);
else
cur[lson] = pre[lson], Insert(cur[rson], pre[rson], mid + 1, r, pos);
Pushup(cur, l, r);
}

void Update(int& cur, int pre, int l, int r, int x, int y)
{
if (x <= l and r <= y)
{
cur = 0;
return;
}
cur = ++ncnt;
cur[lson] = pre[lson], cur[rson] = pre[rson], cur[val] = pre[val];
int mid = (l + r) >> 1;
if (x <= mid)
Update(cur[lson], pre[lson], l, mid, x, y);
if (mid < y)
Update(cur[rson], pre[rson], mid + 1, r, x, y);
Pushup(cur, l, r);
}

inline int Query(int cur, int pos)
{
int l = 0, r = lim, mid;
while (l < r)
{
mid = (l + r) >> 1;
if (pos <= mid)
cur = cur[lson], r = mid;
else
cur = cur[rson], l = mid + 1;
}
return cur[val];
}

inline int _find(int cur, int l, int r) // when there is, find the left 0 in [l, r]
{
int mid;
while (l < r)
{
mid = (l + r) >> 1;
if (cur[lson][val] == Mod(pw2[mid - l + 1] - 1))
l = mid + 1, cur = cur[rson];
else
r = mid, cur = cur[lson];
}
return l;
}

int Find(int cur, int l, int r, int x, int y)
{
if (x <= l and r <= y)
{
if (cur[val] == Mod(pw2[r - l + 1] - 1))
return -1;
return _find(cur, l, r);
}
int mid = (l + r) >> 1, ans = -1;
if (x <= mid)
ans = Find(cur[lson], l, mid, x, y);
if (ans == -1 and mid < y)
ans = Find(cur[rson], mid + 1, r, x, y);
return ans;
}

inline bool Cmp(int x, int y)
{
int l = 0, r = lim, mid;
while (l < r)
{
mid = (l + r) >> 1;
if (x[rson][val] == y[rson][val])
x = lson[x], y = lson[y], r = mid;
else
x = rson[x], y = rson[y], l = mid + 1;
}
if (x[val] == y[val])
return x < y;
else
return x[val] < y[val];
}
}

struct Info
{
int u, root;

Info(int _u, int _root) : u(_u), root(_root) { }

bool operator<(const Info& a) const
{ return SEG::Cmp(a.root, root); }
};

int main()
{
#ifndef ONLINE_JUDGE
freopen("physics.in", "r", stdin);
freopen("physics.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> id >> n >> m >> S >> T;
pw2[0] = 1;
for (int i = 1; i <= lim; ++i)
pw2[i] = Mod(pw2[i - 1] << 1);
while (m--)
{
int u, v, w;
cin >> u >> v >> w;
G[u].emplace_back(v, w), G[v].emplace_back(u, w);
}
for (int i = 1; i <= n; ++i)
if (i != S)
SEG::Insert(SEG::root[i], 0, 0, lim, lim); // dis[i] = INF
q.push(Info(S, SEG::root[S]));
while (!q.empty())
{
int u = q.top().u;
q.pop();
if (vis[u])
continue;
vis.set(u);
for (auto i : G[u])
{
int v = i.first, w = i.second, tmp = 0, cur = 0;
if (SEG::Query(SEG::root[u], w))
{
int pos = SEG::Find(SEG::root[u], 0, lim, w, lim);
SEG::Update(tmp, SEG::root[u], 0, lim, w, pos - 1);
SEG::Insert(cur, tmp, 0, lim, pos);
}
else
SEG::Insert(cur, SEG::root[u], 0, lim, w);
if (SEG::Cmp(cur, SEG::root[v]))
SEG::root[v] = cur, q.push(Info(v, SEG::root[v]));
}
}
if (!vis[T])
cout << "-1" << endl;
else
cout << SEG::root[T][SEG::val] << endl;
return 0;
}