久未放晴的天空|

TulipeNoire

园龄:1年10个月粉丝:18关注:17

2022-2023 集训队互测 Round 6 - >.<

不能包含某一条路径,这个东西看起来很像字符串啊!我们把这些路径插入到 trie 中,建立 AC 自动机,然后再把 n 个单点插进去。在建出来的 AC 自动机上跑最短路,钦定某些点不能被进入即可。但是因为字符集是 O(n) 的,所以直接暴力连边复杂度无法接受。

考虑连边的过程,是继承 fail 树父亲的边,再更新它到它在 trie 树上的后继所对应的边。我们就可以发现这个连边其实是可以用可持久化线段树来维护的,支持继承操作和修改操作。这一部分的话时间就是 O((n+m+L)log(n+m+L)) 的了。但是因为边的数量还是很多,所以最短路算法的时间还是难以承受。

既然这些边能在可持久化线段树中用 O((n+m+L)logn) 个节点存下来,那么为什么要暴力地遍历 O(n2) 次呢?我们知道,Dijkstra 算法中每次从优先队列中取出的距离是单调不降的。这意味着,某个节点利用可持久化线段树上的一个点上的信息进行松弛对应节点后,下一次另一个节点来松弛时,肯定不会造成任何影响,因为对应的被松弛节点和边权都是固定的,而后面的距离肯定不比前面小,所以后面的松弛是无用的。我们在优先队列中取出一个节点后,遍历这个节点(版本)所对应的可持久化线段树,遇到被标记过的点就退出,再用被遍历到的边松弛,并把这整棵树没有被标记的地方打上标记。那么一条边至多松弛一次,可持久化线段树的每个节点只被遍历一次,故至此我们用 O((n+m+L)log(n+m+L)) 的复杂度解决了此问题。

细节比较多,比如被钦定不能被进入的点要沿 fail 树下传(ACAM 老生常谈的挂法),以及一条边如果在原图中是不存在的,也需要注意不能存在于新图中。

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int N = 200005;
const LL inf = 1e18;
int n, m, k, tot, pos[N], id[N << 1];
set<pii> G[N], ch[N << 1];
bool ban[N << 1];
int cnt, fail[N << 1], root[N << 1];
struct Node {
    int lc, rc, to, w;
    bool ban;
} tr[N << 6];
int update(int now, int l, int r, int x, int to, int w) {
    int p = ++cnt;
    tr[p] = tr[now];
    if (l == r) {
        tr[p].to = to, tr[p].w = w;
        return p;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) tr[p].lc = update(tr[now].lc, l, mid, x, to, w);
    else tr[p].rc = update(tr[now].rc, mid + 1, r, x, to, w);
    return p;
}
int query(int p, int l, int r, int x) {
    if (l == r)
        return tr[p].to;
    int mid = (l + r) >> 1;
    if (x <= mid)
        return query(tr[p].lc, l, mid, x);
    return query(tr[p].rc, mid + 1, r, x);
}
inline void build() {
    queue<int> q;
    for (auto x : ch[0]) q.push(x.se);
    while (!q.empty()) {
        int p = q.front();
        q.pop();
        ban[p] |= ban[fail[p]];
        int now = root[fail[p]];
        if (!fail[p]) for (auto x : G[id[p]]) now = update(now, 1, n, x.fi, pos[x.fi], x.se);
        for (auto x : ch[p]) {
            auto it = G[id[p]].lower_bound({x.fi, 0});
            if (it != G[id[p]].end() && it->fi == x.fi) now = update(now, 1, n, x.fi, x.se, it->se);
        }
        root[p] = now;
        for (auto x : ch[p]) {
            fail[x.se] = query(root[fail[p]], 1, n, x.fi);
            if (!fail[x.se]) fail[x.se] = pos[x.fi];
            q.push(x.se);
        }
    }
}
bool vis[N << 1];
LL dis[N << 1];
priority_queue<pair<LL, int>> q;
int nowp;
void solve(int p, int l, int r) {
    if (tr[p].ban) return;
    tr[p].ban = 1;
    if (l == r) {
        if (!ban[tr[p].to] && !vis[tr[p].to]) {
            LL dis0 = dis[nowp] + tr[p].w;
            if (dis[tr[p].to] > dis0) dis[tr[p].to] = dis0, q.push({-dis0, tr[p].to});
        }
        return;
    }
    int mid = (l + r) >> 1;
    if (tr[p].lc) solve(tr[p].lc, l, mid);
    if (tr[p].rc) solve(tr[p].rc, mid + 1, r);
}
inline bool Dijkstra() {
    memset(dis, 63, sizeof dis);
    q.push({0, pos[1]});
    dis[pos[1]] = 0;
    while (!q.empty()) {
        int p = q.top().se;
        if (id[p] == n) {
            printf("%lld", -q.top().fi);
            return 1;
        }
        q.pop();
        if (vis[p]) continue;
        vis[p] = 1;
        nowp = p, solve(root[p], 1, n);
    }
    return 0;
}
int main() {
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        G[u].insert({v, w});
    }
    for (int i = 1; i <= k; i++) {
        int l;
        scanf("%d", &l);
        int now = 0;
        while (l--) {
            int x;
            scanf("%d", &x);
            auto it = ch[now].lower_bound({x, 0});
            int nxt;
            if (it == ch[now].end() || it -> fi != x) ch[now].insert({x, nxt = ++tot}), id[tot] = x;
            else nxt = it -> se;
            now = nxt;
        }
        ban[now] = 1;
    }
    for (int i = 1; i <= n; i++) {
        auto it = ch[0].lower_bound({i, 0});
        if (it == ch[0].end() || it -> fi != i) ch[0].insert({i, ++tot}), pos[i] = tot, id[tot] = i;
        else pos[i] = it -> se;
    }
    build();
    if (!Dijkstra()) puts("-1");
    return 0;
}

本文作者:TulipeNoire

本文链接:https://www.cnblogs.com/TulipeNoire/p/18664363/2022_2023_Homework_R6T3

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   TulipeNoire  阅读(25)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起