[BZOJ1576] [BZOJ3694] [USACO2009Jan] secure path (a shortest path tree chain split +)

[BZOJ1576] [BZOJ3694] [USACO2009Jan] secure path (a shortest path tree chain split +)

Face questions

BZOJ1576 and BZOJ3694 almost exactly the same, just BZOJ3694 given directly to the shortest path tree

No points are given a n m to FIG edges, points from the number n 1 ~ n, is defined as a point source. Shortest Path Tree is defined as follows: from the source 1 through the current collector side to an arbitrary point T i and only one path, and this path is the shortest path across FIG. 1 to i, T constituting the shortest edge set tree. Gives the shortest path tree, find for each point i in addition to a source, find the shortest path, without passing through the last edge in claim shortest path tree given 1 to i.

analysis

The procedure of finding the shortest path tree slightly.

After a point i deleted the tree to his father's side (ie 1 i path to the last side), we must go through to reach the non-tree edge i. Greedy reasons, only after a non-tree edge is clearly the best.

graph.png

For a non-tree edge (x, y) [FIG dotted blue], which corresponds to a path (x, y) in the tree. For point z on this path, it is removed to the father's side in z we can go this path 1-> x-> y-> z. 1-> apparently distance x \ (dist (x) \) distance, x-> y is \ (len (x, Y) \) , from y-> z in [Green Line] is \ (dist [ Y] -dist [z] \) . Therefore the path length is z \ (dist [x] + dist [y] + len (x, y) -dist [z] \)

Note that when z = lca (x, y), not from 1-> x-> y-> z, as z and x can not reach the father's side are deleted.

The method is very clear. For each edge, we \ (dist [x] + dist [y] + len (x, y) \) to update the path (x, y) (not included LCA) on points, each point \ (Z \) obtaining \ (min (dist [X] + dist [Y] + len (X, Y)) \) . Finally, output \ (min (dist [x] + dist [y] + len (x, y)) - dist [z] \) to. Path modification, query a single point, the tree may be split chain solution. Time complexity \ (O (n \ log ^ 2 n) \)

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 4000
#define maxm 100000 
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;

int n,m;
struct edge{
    int from;
    int to;
    int next;
    int len;
    int type;
}E[maxm*2+5];
int head[maxn+5];
int esz=1;
void add_edge(int u,int v,int w,int t){
    esz++;
    E[esz].from=u;
    E[esz].to=v;
    E[esz].next=head[u];
    E[esz].len=w;
    E[esz].type=t;
    head[u]=esz;
}

struct node{
    int id;
    ll dist;
    node(){
        
    }
    node(int _id,ll _dist){
        id=_id;
        dist=_dist;
    }
    friend bool operator < (node p,node q){
        return p.dist>q.dist;
    }
};
bool vis[maxn+5];
ll dist[maxn+5];
void dijkstra(int s){
    priority_queue<node>q;
    memset(vis,0,sizeof(vis));
    memset(dist,0x3f,sizeof(dist));
    dist[s]=0;
    q.push(node(s,0));
    while(!q.empty()){
        int x=q.top().id;
        q.pop();
        if(vis[x]) continue;
        vis[x]=1;
        for(int i=head[x];i;i=E[i].next){
            int y=E[i].to;
            if(dist[y]>dist[x]+E[i].len){
                dist[y]=dist[x]+E[i].len;
                if(!vis[y]) q.push(node(y,dist[y]));
            }
        }
    }
}

vector<int>T[maxn+5];
int deep[maxn+5];
int sz[maxn+5];
int fa[maxn+5];
int son[maxn+5];
int top[maxn+5];
int dfn[maxn+5];
int tim;
void dfs1(int x,int f){
    sz[x]=1;
    fa[x]=f;
    deep[x]=deep[f]+1;
    for(int i=0;i<T[x].size();i++){
        int y=T[x][i];
        if(y!=f){
            dfs1(y,x);
            sz[x]+=sz[y];
            if(sz[y]>sz[son[x]]) son[x]=y;
        }
    }
}
void dfs2(int x,int t){
    top[x]=t;
    dfn[x]=++tim;
    if(son[x]) dfs2(son[x],t);
    for(int i=0;i<T[x].size();i++){
        int y=T[x][i];
        if(y!=fa[x]&&y!=son[x]){
            dfs2(y,y);
        }
    }
}

struct segment_tree{
    struct node{
        int l;
        int r;
        ll val;
        ll mark;
    }tree[maxn*4+5];
    void push_up(int pos){
        tree[pos].val=max(tree[pos<<1].val,tree[pos<<1|1].val);
    }
    void build(int l,int r,int pos){
        tree[pos].l=l;
        tree[pos].r=r;
        tree[pos].mark=INF;
        tree[pos].val=INF;
        if(l==r) return;
        int mid=(l+r)>>1;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
        push_up(pos);
    }
    void push_down(int pos){
        if(tree[pos].mark!=INF){
            tree[pos<<1].mark=min(tree[pos<<1].mark,tree[pos].mark);
            tree[pos<<1|1].mark=min(tree[pos<<1|1].mark,tree[pos].mark);
            tree[pos<<1].val=min(tree[pos<<1].val,tree[pos].mark);
            tree[pos<<1|1].val=min(tree[pos<<1|1].val,tree[pos].mark);
            tree[pos].mark=INF;
        }
    } 
    void update(int L,int R,ll uval,int pos){
        if(L<=tree[pos].l&&R>=tree[pos].r){
            tree[pos].mark=min(tree[pos].mark,uval);
            tree[pos].val=min(tree[pos].val,uval);
            return;
        }
        push_down(pos);
        int mid=(tree[pos].l+tree[pos].r)>>1;
        if(L<=mid) update(L,R,uval,pos<<1);
        if(R>mid) update(L,R,uval,pos<<1|1);
        push_up(pos);
    }
    ll query(int L,int R,int pos){
        if(L<=tree[pos].l&&R>=tree[pos].r){
            return tree[pos].val;
        }
        push_down(pos);
        int mid=(tree[pos].l+tree[pos].r)>>1;
        ll ans=INF;
        if(L<=mid) ans=min(ans,query(L,R,pos<<1));
        if(R>mid) ans=min(ans,query(L,R,pos<<1|1));
        return ans;
    }
}S;

int lca(int x,int y){
    while(top[x]!=top[y]){
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}
void update(int x,int y,ll val){
//  printf("update %d->%d val=%d\n",x,y,val);
    int tx=top[x],ty=top[y];
    while(tx!=ty){
        if(deep[tx]<deep[ty]){
            swap(tx,ty);
            swap(x,y);
        }
        S.update(dfn[tx],dfn[x],val,1);
        x=fa[tx];
        tx=top[x];
    }
    if(deep[x]>deep[y]) swap(x,y);
    S.update(dfn[son[x]],dfn[y],val,1);//注意是son[x]而不是x,因为不包含lca
}

int main(){
    int u,v,w,t;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d %d %d",&u,&v,&w,&t);
        add_edge(u,v,w,t);
        add_edge(v,u,w,t);
        if(t==1){
            T[u].push_back(v);
            T[v].push_back(u);
        }
    }
    dijkstra(1);
    dfs1(1,0);
    dfs2(1,0);
    S.build(1,n,1); 
    for(int i=2;i<=esz;i+=2){
        if(E[i].type==0){
            int x=E[i].from;
            int y=E[i].to;
            update(x,y,dist[x]+dist[y]+E[i].len);
        }
    }
    for(int i=2;i<=n;i++){
        ll ans=S.query(dfn[i],dfn[i],1);
        if(ans==INF) printf("-1 ");
        else printf("%lld ",ans-dist[i]);
    }
}

Guess you like

Origin www.cnblogs.com/birchtree/p/11966151.html