【BZOJ2125】最短路(仙人掌,圆方树)

Description

给一个N个点M条边的连通无向图,满足每条边最多属于一个环,有Q组询问,每次询问两点之间的最短路径。


Solution

建出圆方树。对于圆圆边,边权为原仙人掌的边权;对于圆方边,边权为圆点到方点代表的环中DFS序最小的点的距离

对于每个询问,如果LCA为圆点,那么答案为两点距离;如果是方点,答案为两点到方点代表的环的距离和加上两点走到环上后所在的点的最短距离。


Code

/**************************************
 * Au: Hany01
 * Prob: BZOJ2125 最短路(圆方树)
 * Date: Jul 30th, 2018
 * Email: hany01@foxmail.com
**************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.beg1in(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define x first
#define y second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    register char c_; register int _, __;
    for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1e4 + 5, maxm = maxn << 2;

int n, e1 = 1, e2 = 1, beg1[maxn], nex1[maxm], v1[maxm], w1[maxm], beg2[maxn << 1], nex2[maxm], v2[maxm], dep[maxn << 1], fa1[maxn], fa2[15][maxn << 1], dfn[maxn], low[maxn], clk, tot;
LL dis1[maxn], dis2[maxn << 1], w2[maxm], cir[maxn];

inline void add1(int uu, int vv, int ww) { v1[++ e1] = vv, w1[e1] = ww, nex1[e1] = beg1[uu], beg1[uu] = e1; }
inline void add2(int uu, int vv, LL ww) { v2[++ e2] = vv, w2[e2] = ww, nex2[e2] = beg2[uu], beg2[uu] = e2; }

inline void Build(int u, int anc, LL dsum) {
    static LL d; ++ tot;
    for (register int t = u; t != fa1[anc]; t = fa1[t])
        cir[t] = dsum, d = min(dis1[t] - dis1[anc], dsum - dis1[t] + dis1[anc]), add2(tot, t, d), add2(t, tot, d);
}

void DFS(int u, int fe) {
    dfn[u] = low[u] = ++ clk;
    for (register int i = beg1[u]; i; i = nex1[i]) if ((i >> 1) != fe) {
        if (!dfn[v1[i]]) dis1[v1[i]] = dis1[u] + w1[i], fa1[v1[i]] = u, DFS(v1[i], i >> 1), chkmin(low[u], low[v1[i]]);
        else chkmin(low[u], dfn[v1[i]]);
        if (dfn[u] < low[v1[i]]) add2(u, v1[i], w1[i]), add2(v1[i], u, w1[i]);
    }
    for (register int i = beg1[u]; i; i = nex1[i])
        if (fa1[v1[i]] != u && dfn[v1[i]] > dfn[u]) Build(v1[i], u, w1[i] + dis1[v1[i]] - dis1[u]);
}

void DFS(int u) {
    for (register int i = beg2[u]; i; i = nex2[i]) if (v2[i] != fa2[0][u])
        fa2[0][v2[i]] = u, dis2[v2[i]] = dis2[u] + w2[i], dep[v2[i]] = dep[u] + 1, DFS(v2[i]);
}

inline int LCA(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    Fordown(i, 14, 0) if (dep[v] + (1 << i) <= dep[u]) u = fa2[i][u];
    if (u == v) return u;
    Fordown(i, 14, 0) if (fa2[i][u] != fa2[i][v]) u = fa2[i][u], v = fa2[i][v];
    return fa2[0][u];
}

inline int climb(int u, int anc) {
    Fordown(i, 14, 0) if (dep[fa2[i][u]] > dep[anc]) u = fa2[i][u];
    return u;
}

inline LL dist(int u, int v) {
    static LL t; t = llabs(dis1[u] - dis1[v]);
    assert(cir[u] == cir[v]);
    return min(t, cir[u] - t);
}

int main()
{
#ifdef hany01
    File("bzoj2125");
#endif

    static int m, q, uu, vv, ww, lca, ua, va;

    tot = n = read(), m = read(), q = read();
    while (m --) uu = read(), vv = read(), ww = read(), add1(uu, vv, ww), add1(vv, uu, ww);

    DFS(1, 0), DFS(1);
    For(j, 1, 14) For(i, 1, tot) fa2[j][i] = fa2[j - 1][fa2[j - 1][i]];

    while (q --) {
        uu = read(), vv = read(), lca = LCA(uu, vv);
        if (lca <= n) printf("%lld\n", dis2[uu] + dis2[vv] - (dis2[lca] << 1));
        else ua = climb(uu, lca), va = climb(vv, lca), printf("%lld\n", dis2[uu] + dis2[vv] - dis2[ua] - dis2[va] + dist(ua, va));
    }

    return 0;
}
发布了393 篇原创文章 · 获赞 410 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览