bzoj3694

/*
 * 对于不在最短路树上的边(x, y)
 *          1
 *          |
 *          |
 *          t
 *         / \
 *        /   \
 *       x-----y
 * 考虑这样一种形态的图, ‘-’ 标记为非最短路树的边
 * 对于边集(x, t)内的任意一点 i, 到达它的所有方式一定是 1 -> t -> y -> x -> i
 * 这样就可以对树边(x, t)标记 Min = dis[y] + dis[x] + W_{x,y}
 * 每个点在标记中取最小
 * Answer_i 就是 Min_i - dis[i] 
 */
#include <bits/stdc++.h>
 
const int N = 4e3 + 10, M = 1e5 + 10;
 
struct Node {
    int u, v, w, nxt;
} G[M << 1], E[M << 1];
int n, m;
int head[N], now, js, dis[N];
 
#define gc getchar()
 
inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}
 
inline void write_int(int x) {
    printf("%d\n", x);
}
 
inline void Add(int u, int v, int w) {
    G[++ now].v = v, G[now].w = w, G[now].nxt = head[u], head[u] = now;
}
 
int fa[N], deep[N], topp[N], size[N], son[N], tree[N], Tree;
 
void Dfs_1(int u, int f_, int dep) {
    fa[u] = f_, deep[u] = dep, size[u] = 1;
    for(int i = head[u]; ~ i; i = G[i].nxt) {
        int v = G[i].v;
        if(v == f_) continue;
        dis[v] = dis[u] + G[i].w;
        Dfs_1(v, u, dep + 1);
        size[u] += size[v];
        if(size[v] > size[son[u]]) son[u] = v;
    }
}
 
void Dfs_2(int u, int tp) {
    topp[u] = tp, tree[u] = ++ Tree;
    if(!son[u]) return ;
    Dfs_2(son[u], tp);
    for(int i = head[u]; ~ i; i = G[i].nxt)
        if(G[i].v != fa[u] && G[i].v != son[u]) Dfs_2(G[i].v, G[i].v);
}
 
const int oo = 999999999;
int Minn[N << 2];
 
#define lson jd << 1
#define rson jd << 1 | 1
 
void Build_tree(int l, int r, int jd) {
    Minn[jd] = oo;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    Build_tree(l, mid, lson), Build_tree(mid + 1, r, rson);
}
 
void Sec_G(int l, int r, int jd, int x, int y, int w) {
    if(x <= l && r <= y) {
        Minn[jd] = std:: min(Minn[jd], w);
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_G(l, mid, lson, x, y, w);
    if(y > mid)  Sec_G(mid + 1, r, rson, x, y, w);
}
 
void Sec_G_imp(int x, int y, int w) {
    int tpx = topp[x], tpy = topp[y];
    while(tpx != tpy) {
        if(deep[tpx] < deep[tpy]) std:: swap(tpx, tpy), std:: swap(x, y);
        Sec_G(1, n, 1, tree[tpx], tree[x], w);
        x = fa[tpx], tpx = topp[x];
    }
    if(x == y) return ;
    if(deep[x] < deep[y]) std:: swap(x, y);
    Sec_G(1, n, 1, tree[y] + 1, tree[x], w);
}
 
int Ans[N];
 
void Dfs_tree(int l, int r, int jd) {
    if(l == r) {
        Ans[l] = Minn[jd];
        return ;
    }
    int mid = (l + r) >> 1;
    Minn[lson] = std:: min(Minn[lson], Minn[jd]);
    Minn[rson] = std:: min(Minn[rson], Minn[jd]);
    Dfs_tree(l, mid, lson), Dfs_tree(mid + 1, r, rson);
}
 
int main() {
    n = read(), m = read();
    for(int i = 1; i <= n; i ++) head[i] = -1;
    for(int i = 1; i <= m; i ++) {
        int u = read(), v = read(), w = read(), opt = read();
        if(opt) Add(u, v, w), Add(v, u, w);
        else E[++ js].u = u, E[js].v = v, E[js].w = w;
    }
    Dfs_1(1, 0, 1);
    Dfs_2(1, 0);
    Build_tree(1, n, 1);
    for(int i = 1; i <= js; i ++) {
        int x = E[i].u, y = E[i].v;
        Sec_G_imp(x, y, dis[x] + dis[y] + E[i].w);
    }
    Dfs_tree(1, n, 1);
    for(int i = 2; i <= n; i ++) {
        if(Ans[tree[i]] == oo) write_int(-1);
        else write_int(Ans[tree[i]] - dis[i]);
    }
    return 0;
}

 

posted @ 2018-08-07 21:03  xayata  阅读(125)  评论(0编辑  收藏  举报