线段树优化 dijkstra (CF787D Legacy)
线段树有很多神奇的作用,优化 \(dijkstra\) 就是其中之一
我们拿这道模板题作为例子
题意:
有 \(n\) 个星球, \(m\) 条路径,每条路径分为下列三种:
- \(u~\rightarrow ~v\),费用 \(w\)
- \(u~\rightarrow ~[l,r]\),费用 \(w\)
- \([l,r]~\rightarrow ~v\),费用 \(w\)
求 \(s\) 星球待其他星球的最短路
跑单源最短路?显然是 \(dijkstra\)
但直接建边的话数组都存不下,那么怎么办呢?
考虑线段树优化最短路建模
建两颗线段树,如下图建边:
显然共 \(O(n~log~n)\) 个点;
\(O(n~log~n+q~log~n)\) 条边
再在上面跑 \(dijkstra\) 就行了
时间复杂度 \(O(n~log^2~n)\)
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
int n, m, s, cnt ;
struct SegTree {
int l, r, in, out ;
vii e ;
#define ls(x) x << 1
#define rs(x) x << 1 | 1
#define l(x) tr[x].l
#define r(x) tr[x].r
#define in(x) tr[x].in
#define out(x) tr[x].out
#define e(x) tr[x].e
} tr[N << 2] ;
void build(int x, int l, int r) {
l(x) = l, r(x) = r ;
if (l == r) {
in(x) = out(x) = l ;
return ;
}
int mid = (l + r) >> 1 ;
build(ls(x), l, mid) ;
build(rs(x), mid + 1, r) ;
out(x) = ++cnt ; in(x) = ++cnt ;
e(out(ls(x))).pb(mp(out(x), 0)) ;
e(out(rs(x))).pb(mp(out(x), 0)) ;
e(in(x)).pb(mp(in(ls(x)), 0)) ;
e(in(x)).pb(mp(in(rs(x)), 0)) ;
}
void modifyin(int x, int l, int r, int pos, int c) {
if (l <= l(x) && r(x) <= r) {
e(pos).pb(mp(in(x), c)) ;
return ;
}
int mid = (l(x) + r(x)) >> 1 ;
if (l <= mid) modifyin(ls(x), l, r, pos, c) ;
if (mid < r) modifyin(rs(x), l, r, pos, c) ;
}
void modifyout(int x, int l, int r, int pos, int c) {
if (l <= l(x) && r(x) <= r) {
e(out(x)).pb(mp(pos, c)) ;
return ;
}
int mid = (l(x) + r(x)) >> 1 ;
if (l <= mid) modifyout(ls(x), l, r, pos, c) ;
if (mid < r) modifyout(rs(x), l, r, pos, c) ;
}
struct node {
int x, dis ;
bool operator < (node a) const {
return dis > a.dis ;
}
};
int dis[N * 10] ;
pq <node> q ;
void dij() {
ass(dis, 0x3f) ; dis[s] = 0 ;
q.push((node){s, 0}) ;
while (!q.empty()) {
node now = q.top() ; q.pop() ;
if (dis[now.x] != now.dis) continue ;
rep(i, 0, siz(e(now.x)) - 1) {
int to = e(now.x)[i].fi, w = e(now.x)[i].se ;
if (w + now.dis < dis[to]) {
dis[to] = now.dis + w ;
q.push((node){to, dis[to]}) ;
}
}
}
}
signed main(){
scanf("%lld%lld%lld", &n, &m, &s) ;
cnt = n ;
build(1, 1, n) ;
rep(i, 1, m) {
int op ; scanf("%lld", &op) ;
if (op == 1) {
int x, y, v ; scanf("%lld%lld%lld", &x, &y, &v) ;
e(x).pb(mp(y, v)) ;
}
else if (op == 2) {
int x, l, r, v ; scanf("%lld%lld%lld%lld", &x, &l, &r, &v) ;
modifyin(1, l, r, x, v) ;
} else {
int x, l, r, v ; scanf("%lld%lld%lld%lld", &x, &l, &r, &v) ;
modifyout(1, l, r, x, v) ;
}
}
dij() ;
rep(i, 1, n) printf("%lld ", (dis[i] == INF) ? -1 : dis[i]) ;
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/
总结这个算法,线段树其实并不是优化时间复杂度的,他只是使得空间与时间平衡,不至于开不下空间,而相应的,他也给时间复杂度多了一个 \(log\)
加油ヾ(◍°∇°◍)ノ゙