【NOI2018】【BZOJ5415】【UOJ393】【LOJ2718】归程

【题目链接】

【前置技能】

  • 最短路
  • Kruskal重构树

【题解】

  • 题意是要求这么一个东西:在边权大于等于给定水位的某个联通块走到 1 1 1号点的最短路。先从 1 1 1号点跑一个单源最短路算法,令点权为最短路的长度,那么就是要求边权大于等于给定水位的某个联通块中的点权最小值。
  • 比较显然的事情就是,有用的边一定是最大生成树上的边。那么按照水位线建立最大生成树,重构树上每个点记录子树中点权的最小值,每次询问的时候在重构树上倍增到最小的大于等于水位的点,这个点的子树中的所有点就是联通块中能到达的点,然后查询子树中的最小值即可。
  • 据说SPFA求最短路会被卡成皮皮哦~
  • 时间复杂度 O ( ( N + M ) l o g N + Q l o g N ) O((N+M)logN+QlogN) O((N+M)logN+QlogN)

【代码】

#include<bits/stdc++.h>
#define INF	0x3f3f3f3f
#define	LL	long long
#define	MAXN	200010
#define	MAXM	400010
#define	MAXLOG	25
using namespace std;
int Q, TOT, n, m, k, s, v, p, lastans, dis[MAXN], vis[MAXN * 2];
struct edg{int u, v, l, h;}e[MAXM];
struct heapnode{int id, d;};
priority_queue <heapnode> heap;
struct info{int v, l;};
vector <info> a[MAXN];

bool operator < (heapnode a, heapnode b){
    return a.d > b.d;
}

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

int cmp(edg a, edg b){
    return a.h > b.h;
}

void dij(){
    for (int i = 1; i <= n; ++i)
        dis[i] = 2 * INF, vis[i] = 0;
    dis[1] = 0;
    while (!heap.empty()) heap.pop();
    heap.push((heapnode){1, 0});
    while (!heap.empty()){
        heapnode cur = heap.top();
        heap.pop();
        int pos = cur.id;
        vis[pos] = 1;
        for (unsigned i = 0, si = a[pos].size(); i < si; ++i){
            int to = a[pos][i].v, l = a[pos][i].l;
            if (dis[pos] + l < dis[to]){
                dis[to] = dis[pos] + l;
                heap.push((heapnode){to, dis[to]});
            }
        }
        while (!heap.empty() && vis[heap.top().id]) heap.pop();
    }
}

struct Kruskal_Tree{
    struct info{int son[2], val;}a[MAXN * 2];
    int fa[MAXN * 2][MAXLOG + 2], maxn[MAXN * 2][MAXLOG + 2], ans[MAXN * 2];
    int cnt;
    struct Union_Set{
        int fa[MAXN * 2];
        void init(int n){
            for (int i = 1; i <= n; ++i)
                fa[i] = i;
        }	
        int find(int x){
            if (fa[x] == x) return x;
            return (fa[x] = find(fa[x]));
        }
    }us;
    void dfs(int pos, int dad){
        if (vis[pos]) {printf("%d\n", pos); return;}
        vis[pos] = 1;
        fa[pos][0] = dad, maxn[pos][0] = a[pos].val;
        for (int i = 1; i <= MAXLOG; ++i)
            fa[pos][i] = fa[fa[pos][i - 1]][i - 1],
            maxn[pos][i] = max(maxn[pos][i - 1], maxn[fa[pos][i - 1]][i - 1]);
        ans[pos] = INF * 2;
        if (pos <= n) ans[pos] = dis[pos];
        if (a[pos].son[0]) dfs(a[pos].son[0], pos), chkmin(ans[pos], ans[a[pos].son[0]]);
        if (a[pos].son[1]) dfs(a[pos].son[1], pos), chkmin(ans[pos], ans[a[pos].son[1]]);
    }
    void build(){
        us.init(n * 2);
        cnt = n;
        for (int i = 1; i <= 2 * n; ++i)
            a[i].son[0] = a[i].son[1] = 0, a[i].val = INF * 2;
        int tmp = 0;
        for (int i = 1; i <= m; ++i){
            int u = e[i].u, v = e[i].v, fu = us.find(u), fv = us.find(v);
            if (fu != fv){
                ++cnt;
                a[cnt].son[0] = fu, a[cnt].son[1] = fv, a[cnt].val = e[i].h;
                us.fa[fv] = us.fa[fu] = cnt;
                ++tmp;
            }
            if (tmp == n - 1) break;
        }
        for (int i = 1; i <= cnt; ++i)
            vis[i] = 0;
        dfs(cnt, 0);
    }
    int jump(int cur, int p){
        for (int i = MAXLOG; i >= 0; --i)
            if (fa[cur][i] != 0 && maxn[cur][i] > p && a[fa[cur][i]].val > p) cur = fa[cur][i];
        return cur;
    }
}T;

void work(){
    read(n), read(m);
    lastans = 0;
    for (int i = 1; i <= n; ++i)
        a[i].clear();
    for (int i = 1; i <= m; ++i){
        read(e[i].u), read(e[i].v), read(e[i].l), read(e[i].h);
        a[e[i].u].push_back((info){e[i].v, e[i].l});
        a[e[i].v].push_back((info){e[i].u, e[i].l});
    }
    dij();
    sort(e + 1, e + 1 + m, cmp);
    T.build();
    read(Q), read(k), read(s);
    while (Q--){
        read(v), read(p);
        if (k){
            v = (1ll * v + 1ll * lastans - 1) % n + 1;
            p = (1ll * p + 1ll * lastans) % (s + 1);
        }
        int cur = T.jump(v, p);
        lastans = T.ans[cur];
        printf("%d\n", lastans);
    }
}

int main(){
    read(TOT);
    while (TOT--){
        work();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值