• 杭电多校第七场 Traffic Network in Numazu



    Problem Description

    Chika is elected mayor of Numazu. She needs to manage the traffic in this city. To manage the traffic is too hard for her. So she needs your help.
    You are given the map of the city —— an undirected connected weighted graph with N nodes and N edges, and you have to finish Q missions. Each mission consists of 3 integers OP, X and Y.
    When OP=0, you need to modify the weight of the Xth edge to Y.
    When OP=1, you need to calculate the length of the shortest path from node X to node Y.

    Input

    The first line contains a single integer T, the number of test cases.
    Each test case starts with a line containing two integers N and Q, the number of nodes (and edges) and the number of queries. (3≤N≤105)(1≤Q≤105)
    Each of the following N lines contain the description of the edges. The ith line represents the ith edge, which contains 3 space-separated integers ui, vi, and wi. This means that there is an undirected edge between nodes ui and vi, with a weight of wi. (1≤ui,vi≤N)(1≤wi≤105)
    Then Q lines follow, the ith line contains 3 integers OP, X and Y. The meaning has been described above.(0≤OP≤1)(1≤X≤105)(1≤Y≤105)
    It is guaranteed that the graph contains no self loops or multiple edges.

    Output

    For each test case, and for each mission whose OP=1, print one line containing one integer, the length of the shortest path between X and Y.

    Sample Input

    2
    5 5
    1 2 3
    2 3 5
    2 4 5
    2 5 1
    4 3 3
    0 1 5
    1 3 2
    1 5 4
    0 5 4
    1 5 1
    5 3
    1 2 3
    1 3 2
    3 4 4
    4 5 5
    2 5 5
    0 1 3
    0 4 1
    1 1 4

    Sample Output

    5
    6
    6
    6

    分析

    比赛的时候傻了,,把树换成基环树就不会搞了。

    • 先把环中的任意一条边去除,边连接的两点记为r1,r2
    • 每次查询的时候,x与y之间的距离有三个:(1)去除边后树上的最短距离;(2)x->r1->r2->y;(3) x->r2->r1->y。取三个值中的最小值即可

    代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <map>
    #include <cassert>
    #include <cmath>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int maxn=100050;
    int fa[maxn];
    int find(int x) {
        return x==fa[x]?x:(fa[x]=find(fa[x]));
    }
    struct Edge {
        int v,nxt,d;
    }e[maxn*2];
    int h[maxn],tot,n,q;
    void addEdge(int x,int y,int d) {
        e[++tot]=(Edge){y,h[x],d};
        h[x]=tot;
    }
    ll sum[maxn];
    void add(int x,int val) {
        assert(x>0);
        while(x<=n) {
            sum[x]+=val;
            x+=(x&-x);
        }
    }
    ll query(int x) {
        ll ans=0;
        while(x) {
            ans+=sum[x];
            x-=(x&-x);
        }
        return ans;
    }
     
    int f[maxn][20],idx[maxn],cnt,dep[maxn],R[maxn];
    void dfs(int x,int par,int val) {
        fa[x]=f[x][0]=par;
        dep[x]=dep[par]+1;
        idx[x]=++cnt;
        add(cnt,val);
        for(int i = 1; f[x][i-1]; ++i) f[x][i]=f[f[x][i-1]][i-1];
        for(int i = h[x]; i ; i=e[i].nxt) {
            if(par!=e[i].v) dfs(e[i].v,x,e[i].d);
        }
        R[x]=cnt;
        add(cnt+1,-val);
    }
    int lca(int x,int y) {
        if(dep[x]<dep[y]) swap(x,y);
        int h=dep[x]-dep[y];
        for(int i = 19; i >= 0; --i) {
            if(h&(1<<i)) x=f[x][i];
        }
        if(x==y) return x;
        for(int i = 19; i >= 0; --i) {
            if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
        }
        return f[x][0];
    }
    ll dis(int x,int y) {
        int z=lca(x,y);
        return query(idx[x])+query(idx[y])-query(idx[z])*2;
    }
    int X[maxn],Y[maxn],W[maxn];
    int main() {
        int T;
        scanf("%d", &T);
        while(T--) {
            scanf("%d%d", &n,&q);
            for(int i = 0; i <= n; ++i) h[i]=0,fa[i]=i,sum[i]=0;
            cnt=0,tot=0;
            int r1=0,r2=0,bw=0,ID;
            for(int i = 1; i <= n; ++i) {
                int x,y,w;
                scanf("%d%d%d", &x,&y,&w);
                if(find(x)==find(y)) {
                    r1=x,r2=y,bw=w,ID=i;
                }
                else addEdge(x,y,w),addEdge(y,x,w),fa[find(x)]=find(y);
                X[i]=x,Y[i]=y,W[i]=w;
            }
            dfs(1,0,0);
            while(q--) {
                int op,x,y;
                scanf("%d%d%d", &op,&x,&y);
                if(op==0) {
                    if(ID==x) bw=y;
                    else {
                        int u=(fa[X[x]]==Y[x]?X[x]:Y[x]);
                        add(idx[u],-W[x]+y);
                        add(R[u]+1,W[x]-y);
                        W[x]=y;
                    }
                }
                else {
                    ll ans=dis(x,y);
                    ans=min(ans,dis(r1,x)+dis(r2,y)+bw);
                    ans=min(ans,dis(r1,y)+dis(r2,x)+bw);
                    printf("%lld
    ", ans);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    javaweb基础(6)_servlet配置参数
    javaweb基础(5)_servlet原理
    读书笔记:java特种兵(上)
    基础算法(四):海量数据的处理方法
    基础算法(三)动态规划和贪心算法
    基础算法(二):堆排序,快速排序
    基本算法(一):插入排序,归并排序
    JVM基础和调优(六)
    JVM基础和调优(五)
    JVM基础和调优(四)
  • 原文地址:https://www.cnblogs.com/sciorz/p/9473558.html
Copyright © 2020-2023  润新知